Get the current system time and date

Source: Internet
Author: User
Tags time and date

1. Get the current system time and date

//Get system time

NSDate * senddate=[nsdate Date];

NSDateFormatter *dateformatter=[[nsdateformatter alloc] init];

[Dateformatter setdateformat:@"hh:mm"];

NSString * Locationstring=[dateformatter Stringfromdate:senddate];

//[dateformatter setdateformat:@ "Yyyy-mm-dd-hh-mm-ss"];

NSString * Morelocationstring=[dateformatter Stringfromdate:senddate];

Get system date

Nscalendar * Cal=[nscalendar Currentcalendar];

Nsuinteger unitflags=nsdaycalendarunit| Nsmonthcalendarunit| Nsyearcalendarunit;

Nsdatecomponents * conponent= [cal Components:unitflags Fromdate:senddate];

Nsinteger year=[conponent Year];

Nsinteger month=[conponent Month];

Nsinteger Day=[conponent Day];

NSString * nsdatestring= [nsstring stringwithformat:@%4dyear%2d month%2d Day, Year,month,day];

[Dateformatter release];

2. Get NSDate from a string

String stryear="1988";

String strmonth="09";

String strday="18";

String strhour="5";

String strminutes="18";

String strsec="20";

Morelocationstring=[nsstring stringwithformat:@"%s-%s-%s-%s-%s-%s", Stryear.c_str (), StrMonth.c_str (),

Strday.c_str (), Strhour.c_str (), Strminutes.c_str (), Strsec.c_str ()];

Get nsdate based on time string

NSDateFormatter *dateformatter=[[nsdateformatter alloc] init];

[Dateformatter setdateformat:@"Yyyy-mm-dd-hh-mm-ss"];

NSDate * Olddate=[dateformatter datefromstring:morelocationstring];

The nsdate is obtained through the above code.

3. From GMT time, get local time

NSDate * Olddate=[dateformatter datefromstring:morelocationstring];

Nstimeinterval Timezoneoffset=[[nstimezone Systemtimezone] secondsfromgmt];

NSDate * Newdate=[olddate Datebyaddingtimeinterval:timezoneoffset];

Get the current system time and date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.