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