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

//
//  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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值