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);
複製代碼
相關文章
相關標籤/搜索