時(shí)間在線轉(zhuǎn)化:http://tool.chinaz.com/Tools/unixtime.aspx
1.日期格式轉(zhuǎn)字符串
//日期格式轉(zhuǎn)字符串
- (NSString *)dateToString:(NSDate *)date withDateFormat:(NSString *)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSString *strDate = [dateFormatter stringFromDate:date];
return strDate;
}
2.//字符串轉(zhuǎn)日期格式
//字符串轉(zhuǎn)日期格式
- (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSDate *date = [dateFormatter dateFromString:dateString];
NSDate *mmm = [self worldTimeToChinaTime:date];
return mmm;
}
//將世界時(shí)間轉(zhuǎn)化為中國(guó)區(qū)時(shí)間
- (NSDate *)worldTimeToChinaTime:(NSDate *)date
{
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSInteger interval = [timeZone secondsFromGMTForDate:date];
NSDate *localeDate = [date dateByAddingTimeInterval:interval];
return localeDate;
}
3.//date字符串轉(zhuǎn)時(shí)間戳
//date字符串轉(zhuǎn)時(shí)間戳
- (NSInteger)dateStrToTimeInterval:(NSString *)dataStr withDateFormat:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:dataStr];
//時(shí)間轉(zhuǎn)時(shí)間戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
return timeSp;
}
舉例:
NSDate *nowDate = [NSDate date];
NSLog(@"----初始日期:%@-----",nowDate);
NSString *str1 = [self dateToString:nowDate withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"----當(dāng)前日期:%@-----",str1);
NSDate *date = [self stringToDate:str1 withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"----字符日期:%@-----",date);
NSInteger inter = [self dateStrToTimeInterval:str1 withDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"-----時(shí)間戳:%ld-----",inter);