常見(jiàn)的時(shí)間格式是 yyyy-MM-dd hh:mm:ss,下面文章介紹其他類型
原文:如有侵權(quán),回復(fù)告知后刪除
http://blog.csdn.net/macro_13/article/details/41850031?utm_source=tuicool&utm_medium=referral
//
// main.m
// 時(shí)間格式化
//
// Created by Macro on 14-12-10.
// Copyright (c) 2014年 Macro. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const charchar * argv[]) {
@autoreleasepool {
//返回當(dāng)前時(shí)間,以GMT為準(zhǔn)
NSDate * date = [NSDate date];
NSLog(@"%@", date);
//顯示當(dāng)前時(shí)間距離1970-01-01 00:00:00的秒數(shù)
NSLog(@"%.2f", date.timeIntervalSince1970);
//從現(xiàn)在起3600秒時(shí)候的時(shí)間
NSLog(@"%@", [NSDate dateWithTimeIntervalSinceNow:3600]);
//1970-01-01 00:00:00前3600秒的時(shí)間
NSLog(@"%@", [NSDate dateWithTimeIntervalSince1970:-3600]);
//返回一個(gè)很久之后的時(shí)間
NSLog(@"%@", [NSDate distantFuture]);
//返回一個(gè)很久之前的時(shí)間
NSLog(@"%@", [NSDate distantPast]);
//返回當(dāng)前系統(tǒng)時(shí)區(qū)
NSLog(@"%@", [NSTimeZone systemTimeZone]);
//時(shí)間戳 格式化時(shí)間
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"G yyyy-MM-dd E D F w W a z hh:mm:ss.SSS";
NSString * dateStr = [dateFormatter stringFromDate:date];
NSLog(@"%@", dateStr);
/*
G 年代標(biāo)志符
y 年
M 月
d 日
h 時(shí) 在上午或下午 (1~12)
H 時(shí) 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個(gè)星期幾
w 一年中第幾個(gè)星期
W 一月中第幾個(gè)星期
a 上午 / 下午 標(biāo)記符
k 時(shí) 在一天中 (1~24)
K 時(shí) 在上午或下午 (0~11)
z 時(shí)區(qū)
*/
}
return 0;
}