如何获取当天某个时间并与时间戳相互转换

如何获取当天某个时间

  /**
     *  获取当天凌晨时间
     */
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];

    [calendar setTimeZone:gmt];

    NSDate *date = [NSDate date];

    NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:date];

    components.day-=1;

    [components setHour:0];

    [components setMinute:0];

    [components setSecond: 0];

    NSDate *startDate = [calendar dateFromComponents:components];

    NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];

    WSLog(@"%@", endDate);

时间转化为时间戳

   /**
     *  时间转化为时间戳
     */
 NSTimeInterval a=[endDate timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型

   WSLog(@"%@",timeString);

时间戳转化为时间


 /**
   * 获取时间戳
   * 网络请求获取的数据
   * getTime为网上获取的时间戳
   */
 NSTimeInterval _interval=[getTime doubleValue] / 1000.0;//注意这点

 NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];

 [objDateformat setDateFormat:@"yyyy-MM-dd"];

 NSString *dateString = [objDateformat stringFromDate:date];//转化之后结果为:2016-12-05

 NSString *monthString = [dateString substringWithRange:NSMakeRange(5, 2)];//获取其中某个字段
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值