OC中关于时间的几个函数及格式化时间

来源:互联网 发布:开淘宝需要交钱吗 编辑:程序博客网 时间:2024/05/21 15:07
////  main.m//  时间格式化////  Created by Macro on 14-12-10.//  Copyright (c) 2014年 Macro. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {                //返回当前时间,以GMT为准        NSDate * date = [NSDate date];        NSLog(@"%@", date);                //显示当前时间距离1970-01-01 00:00:00的秒数        NSLog(@"%.2f", date.timeIntervalSince1970);                //从现在起3600秒时候的时间        NSLog(@"%@", [NSDate dateWithTimeIntervalSinceNow:3600]);                //1970-01-01 00:00:00前3600秒的时间        NSLog(@"%@", [NSDate dateWithTimeIntervalSince1970:-3600]);                //返回一个很久之后的时间        NSLog(@"%@", [NSDate distantFuture]);                //返回一个很久之前的时间        NSLog(@"%@", [NSDate distantPast]);                //返回当前系统时区        NSLog(@"%@", [NSTimeZone systemTimeZone]);                        //时间戳  格式化时间        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 年代标志符         y 年         M 月         d 日         h 时 在上午或下午 (1~12)         H 时 在一天中 (0~23)         m 分         s 秒         S 毫秒         E 星期         D 一年中的第几天         F 一月中第几个星期几         w 一年中第几个星期         W 一月中第几个星期         a 上午 / 下午 标记符          k 时 在一天中 (1~24)         K 时 在上午或下午 (0~11)         z 时区         */    }    return 0;}

1 0
原创粉丝点击