iOS 本地時間 / UTC時間 / 時間戳等操作 / 獲取當前年月日


 //獲得當前時間並且轉為字符串

復制代碼
復制代碼
- (NSString *)dateTransformToTimeString
{
    NSDate *currentDate = [NSDate date];//獲得當前時間為UTC時間 2014-07-16 07:54:36 UTC  (UTC時間比標准時間差8小時)
    //轉為字符串
    NSDateFormatter*df = [[NSDateFormatter alloc]init];//實例化時間格式類
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//格式化
    //2014-07-16 07:54:36(NSString類)
    NSString *timeString = [df stringFromDate:currentDate];
    return timeString;
}
復制代碼
復制代碼

 //獲取當前時間轉為時間戳

復制代碼
- (NSString *)dateTransformToTimeSp
{
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;//客戶端當前13位毫秒級時間戳
    NSString *timeSp = [NSString stringWithFormat:@"%llu",recordTime];//時間戳轉字符串(13位毫秒級時間戳字符串)
    return timeSp;
}
復制代碼
復制代碼
復制代碼
 1 //時間戳字符串1469193006001(毫秒)1469193006.001(毫秒,1469193006001234(微秒)1469193006.001234(微秒)轉 UTC時間2016-08-11T07:00:55.611Z
 2 - (NSString *)timespToUTCFormat:(NSString *)timesp
 3 {
 4     NSString *timeString = [timesp stringByReplacingOccurrencesOfString:@"." withString:@""];
 5     if (timeString.length >= 10) {
 6         NSString *second = [timeString substringToIndex:10];
 7         NSString *milliscond = [timeString substringFromIndex:10];
 8         NSString * timeStampString = [NSString stringWithFormat:@"%@.%@",second,milliscond];
 9         NSTimeInterval _interval=[timeStampString doubleValue];
10         NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
11 
12         NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
13         NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
14         [dateFormatter setTimeZone:timeZone];
15         [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
16         NSString *dateString = [dateFormatter stringFromDate:date];
17 
18         return dateString;
19     }
20     return @"";
21 }
復制代碼
復制代碼

//13位時間戳1469193006001(毫秒)轉 系統時間2016-08-11 08:55:36

復制代碼
復制代碼
 1 + (NSString *)timespToYMDFormat:(NSNumber *)timesp
 2 {
 3     NSString *stime = [timesp stringValue];
 4     NSTimeInterval time = [[stime substringToIndex:10] doubleValue];
 5     NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
 6     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 7     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 8 
 9     return [dateFormatter stringFromDate: detaildate];
10 }
復制代碼
復制代碼

//時間轉時間戳的方法:sendDate為NSDate類
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[sendDate timeIntervalSince1970]];

 如果只獲取當前的年月日,用NSDate 直接截取是不對的,以下方法提供了獲取當前的年月日等等

復制代碼
 // 獲取代表公歷的NSCalendar對象
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 獲取當前日期
    // 定義一個時間字段的旗標,指定將會獲取指定年、月、日、時、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 獲取不同時間字段的信息
    NSDateComponents* comp = [gregorian components: unitFlags
                                          fromDate:localeDate];
    NSInteger year = comp.year;

//下面是可以獲取的內容 //
@property NSInteger era;
@property NSInteger year;
@property NSInteger month;
@property NSInteger day;
@property NSInteger hour;
@property NSInteger minute;
@property NSInteger second;
@property NSInteger nanosecond NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekday;
@property NSInteger weekdayOrdinal;
@property NSInteger quarter NS_AVAILABLE(10_6, 4_0);
@property NSInteger weekOfMonth NS_AVAILABLE(10_7, 5_0);
@property NSInteger weekOfYear NS_AVAILABLE(10_7, 5_0);
@property NSInteger yearForWeekOfYear NS_AVAILABLE(10_7, 5_0);
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM