NSDate 北京时间

来源:互联网 发布:python sha.new 编辑:程序博客网 时间:2024/06/04 23:19

   NSString *currentDate = [selfcurrentDateWithTimeZone:@"BJ" date:[NSDate date]];


- (NSString *)currentDateWithTimeZone:(NSString *)zone date:(NSDate *)sourceDate {

    

    NSTimeZone* sourceTimeZone = [NSTimeZonetimeZoneWithAbbreviation:zone];

    

    NSTimeZone* destinationTimeZone = [NSTimeZonesystemTimeZone];

    

    NSInteger sourceGMTOffset = [sourceTimeZonesecondsFromGMTForDate:sourceDate];

    

    NSInteger destinationGMTOffset = [destinationTimeZonesecondsFromGMTForDate:sourceDate];

    

    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

    

    NSDate* destinationDate = [[[NSDatealloc] initWithTimeInterval:intervalsinceDate:sourceDate] autorelease];

    

    NSString *dateStr = [NSStringstringWithFormat:@"%@", destinationDate];

    

    NSString *currentStr = [dateStr substringToIndex:10]; 取前十位  xxxx-xx-xx

    

    return currentStr;

}


原创粉丝点击