??時(shí)間戳轉(zhuǎn)化為時(shí)間??
//將時(shí)間戳轉(zhuǎn)換為時(shí)間 (以毫秒為單位)
//參數(shù):時(shí)間戳
//返回值格式:2019-04-19 10:33:35.886
- (NSString *)getTimestamp:(NSString*)mStr{
NSTimeInterval interval =[mStr doubleValue] / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
NSString *dateString = [formatter stringFromDate: date];
NSLog(@"時(shí)間戳對(duì)應(yīng)的時(shí)間是:%@",dateString);
return dateString;
}
//獲取當(dāng)前時(shí)間 (以毫秒為單位)
//返回值格式:2019-04-19 10:33:35.886
- (NSString *)getNowTimeTimestamp{
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
NSString *dateString = [formatter stringFromDate: datenow];
NSLog(@"當(dāng)前時(shí)間戳對(duì)應(yīng)的時(shí)間是:%@",dateString);
return dateString;
}
??時(shí)間轉(zhuǎn)化為時(shí)間戳??
//將時(shí)間轉(zhuǎn)化成 時(shí)間戳
//返回值格式:1555642454396
- (NSInteger)timeSwitchTimestamp:(NSString *)formatTime{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; //(@"YYYY-MM-dd hh:mm:ss") ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate* date = [formatter dateFromString:formatTime]; //------------將字符串按formatter轉(zhuǎn)成nsdate
//時(shí)間轉(zhuǎn)時(shí)間戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]*1000] integerValue];
NSLog(@"將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳timeSp:%ld",(long)timeSp); //時(shí)間戳的值
return timeSp;
}
//獲取當(dāng)前時(shí)間戳 (以毫秒為單位)
//返回值格式:1555642454396
//這里注意要獲取完全部數(shù)值后在(long)強(qiáng)轉(zhuǎn), 如果(long)[datenow timeIntervalSince1970]*1000 這么寫是先強(qiáng)轉(zhuǎn)在*1000,就是秒級(jí)別的數(shù)據(jù)了
- (NSString *)nowTimeSwitchTimestamp{
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間
//這里如果long不夠用,就用(long long)
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];
return timeSp;
}