//获取当前系统时间的时间戳
#pragma mark - 获取当前时间的 时间戳
+(NSInteger)getNowTimestamp
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
//设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//设置时区,这个对于时间的处理有时很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
//现在时间
NSDate *datenow = [NSDate date];
NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
return timeSp;
}
//将某个时间转化成时间戳
#pragma mark - 将某个时间转化成时间戳
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
//(@"YYYY-MM-dd hh:mm:ss")设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
[formatter setDateFormat:format];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
//将字符串按formatter转成nsdate
NSDate* date = [formatter dateFromString:formatTime];
//时间转时间戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
return timeSp;
}
//将某个时间戳转化成时间
#pragma mark - 将某个时间戳转化成 时间
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
//(@"YYYY-MM-dd hh:mm:ss")设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
[formatter setDateFormat:format];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSLog(@"1296035591 = %@",confromTimesp);
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
//NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
return confromTimespStr;
}