zoukankan      html  css  js  c++  java
  • 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);
    复制代码

     

  • 相关阅读:
    MySQL数据库封装和分页查询
    程序员的价值在哪里?
    奇葩的程序员
    京东咚咚架构演进
    程序员必看的《黑客帝国》,你看懂了吗?
    微信后台技术“干货们”带来的启发
    drf框架 2 drf框架的请求生命周期(as_view和dispatch方法), 请求、解析、渲染、响应、异常, 序列化组件 ,ORM配置回顾(media文件配置),应用在settings.py中INSTALLED_APPS注册意义 ,数据库配置
    drf框架, 接口(api) Django FBV => CBV drf框架的基础试图类 drf核心组件 群查与单查 python换源
    前端Vue框架 05 第三方插件(vuex: 组件间交互的(移动端), axios
    前端Vue框架 04 路由:逻辑跳转、路由传参 项目组件的数据局部化处理data(){ return{} } 组件的生命周期钩子 组件间通信 全局配置css, js
  • 原文地址:https://www.cnblogs.com/xieyulin/p/7060504.html
Copyright © 2011-2022 走看看