注意獲取國際服時(shí)間需要導(dǎo)入三方庫,Demo地址請點(diǎn)擊下方鏈接
DEMO鏈接地址
工具類.h文件
#import <Foundation/Foundation.h>
#import "ios-ntp.h"
@interface HDateChange : NSObject
/**獲取當(dāng)前時(shí)間的 時(shí)間戳*/
+(long long)getNowTimestamp;
/**將某個(gè)時(shí)間戳轉(zhuǎn)化成 時(shí)間*/
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format;
/**將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳*/
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format;
/** NSDate轉(zhuǎn)時(shí)間戳*/
+(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime;
/**時(shí)間戳轉(zhuǎn)NSDate*/
+(NSDate *)timeValueTODate:(long)timeValue;
/**從國際服務(wù)器來獲取網(wǎng)絡(luò)時(shí)間*/
+ (NSDate *)getInternetDate;
@end
工具類.m文件
#import "HDateChange.h"
@implementation HDateChange
#pragma mark - 獲取當(dāng)前時(shí)間的 時(shí)間戳
+(long long)getNowTimestamp{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
//設(shè)置時(shí)區(qū),這個(gè)對于時(shí)間的處理有時(shí)很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間
NSLog(@"設(shè)備當(dāng)前的時(shí)間:%@",[formatter stringFromDate:datenow]);
//時(shí)間轉(zhuǎn)時(shí)間戳的方法:
NSTimeInterval interval = [datenow timeIntervalSince1970];
long long totalMilliseconds = interval*1000 ;
// NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
NSLog(@"設(shè)備當(dāng)前的時(shí)間戳:%ld",(long)totalMilliseconds); //時(shí)間戳的值
return totalMilliseconds;
}
#pragma mark - 將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; //(@"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]] integerValue];
NSLog(@"將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳&&&&&&&timeSp:%ld",(long)timeSp); //時(shí)間戳的值
return timeSp;
}
#pragma mark - 將某個(gè)時(shí)間戳轉(zhuǎn)化成 時(shí)間
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
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;
}
+(long long)getDateTimeTOMilliSeconds:(NSDate *)datetime
{
NSTimeInterval interval = [datetime timeIntervalSince1970];
NSLog(@"轉(zhuǎn)換的時(shí)間戳=%f",interval);
long long totalMilliseconds = interval*1000 ;
NSLog(@"totalMilliseconds=%llu",totalMilliseconds);
return totalMilliseconds;
}
+(NSDate *)timeValueTODate:(long)timeValue{
long date = timeValue / 1000;
NSDate *dates = [NSDate dateWithTimeIntervalSince1970:date];
return dates;
}
+ (NSDate *)getInternetDate
{
NSDate *todayNet = [NSDate networkDate];
return todayNet;
}
@end