//获得系统时间
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY:MM:dd:HH:mm:ss"];
NSString * locationString=[dateformatter stringFromDate:senddate];
NSLog(@"%@",locationString);
//[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
//NSString * morelocationString=[dateformatter stringFromDate:senddate];
<pre name="code" class="objc">// 2. 从字符串来获得NSDate
NSString *strYear=@"1988";
NSString *strMonth=@"09";
NSString *strDay=@"17";
NSString *strHour=@"5";
NSString *strMinutes=@"18";
NSString *strSec=@"20";
NSString * morelocationString=[NSString stringWithFormat:@"%@-%@-%@-%@-%@-%@",strYear,strMonth,strDay,strHour,strMinutes,strSec];
//根据时间字符串获得NSDate
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
// NSDate * oldDate=[dateformatter dateFromString:morelocationString];
// //转换成东八区的时间
// NSDate * data = [NSDate dateWithTimeInterval:3600*8 sinceDate:oldDate];
// NSLog(@"%@",data);
//获得系统日期
NSDate * senddate=[NSDate date];
NSCalendar * cal=[NSCalendar currentCalendar];
NSUInteger unitFlags=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit|NSHourCalendarUnit;
// NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
NSDateComponents * conponent= [cal components:unitFlags fromDate:[NSDate dateWithTimeIntervalSince1970:3600*24*7]];
NSInteger year=[conponent year];
NSInteger month=[conponent month];
NSInteger day=[conponent day];
NSInteger hour = [conponent hour];
NSInteger week = [conponent weekday];
NSString * nsDateString= [NSString stringWithFormat:@"%ld年-%ld月-%ld日-%ld周几",year,month,day,week];
NSLog(@"%@",nsDateString);
// 3. 从GMT时间,得到本地时间
NSDate * oldDate=[dateformatter dateFromString:morelocationString];
NSTimeInterval timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
NSDate * newDate=[oldDate dateByAddingTimeInterval:timeZoneOffset];
NSLog(@"%@",newDate);