iOS 獲取國際服務(wù)器時(shí)間,NSDate與時(shí)間戳相互轉(zhuǎn)換

注意獲取國際服時(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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容