zoukankan      html  css  js  c++  java
  • 【ios】关于ios里时间与日期

    NSDate *currentDate = [NSDate date];//获取当前时间,日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY/MM/dd hh:mm:ss SS"];//hh表示的是十二进制时间
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    NSLog(@"dateString:%@",dateString);

    以上可以把date类型的时间按照指定格式转换成字符串类型。

    同样的,使用dateFromString方法可以将时间以字符串类型转换成date类型

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

    [formatter setDateStyle:NSDateFormatterFullStyle];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *timeString = @"2016-05-16 21:00:00";

     NSDate *date = [formatter dateFromString:timeString];

    在开发中,很多情况存在,服务器那边需要的是CST时间,而以上方法产生的是UTC时间

     

    首先要详细了解在ios中这些类的含义

    NSDate -- 表示一个绝对的时间点,用来表示公历的GMT时间(格林威治时间)
    NSTimeZone -- 时区信息
    NSLocale -- 本地化信息
    NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类
    NSCalendar -- 日历类,它提供了大部分的日期计算接口,并且允许您在NSDate和NSDateComponents之间转换
    NSDateFormatter -- 用来在日期和字符串之间转换

    几个重要方法:

    - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)seconds

    以当前时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds

    - (id)initWithTimeIntervalSince1970:(NSTimeInterval)seconds

    以GMT时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds

    - (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds

    以2001-1-1 0:0:0的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds

     - (id)initWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)refDate

    以基准时间的偏移秒数来初始化,也可以直接调用类方法 + (id)dateWithTimeInterval:(NSTimeInterval)seconds sinceDate:(NSDate *)date

    -(id)dateByAddingTimeInterval:(NSTimeInterval)seconds

    返回以当前NSDate对象为基准,偏移多少秒后得到的新NSDate对象。(旧方法 - (id)addTimeInterval:(NSTimeInterval)seconds已被弃用)

    - (NSTimeInterval)timeIntervalSince1970

    返回当前对象时间与1970-1-1 0:0:0的相隔秒数,也可以这样理解:从1970-1-1 0:0:0开始,经过多少秒到达对象指定时间。

    - (NSTimeInterval)timeIntervalSinceNow

     

    返回当前对象时间与指定时间的相隔秒数,也可以这样理解:从客户端当前时间开始,经过多少秒到达对象指定时间。

    + (NSTimeZone *)systemTimeZone

    返回系统时区

    原文地址:http://blog.sina.com.cn/s/blog_9cd1705d0102v5x4.html

    注:因为UTC格式的时间与需要上传到服务器的CST时间可能存在八小时的时差,所以在格式化的时候需要设置系统时区,最好设置好本地化标识

    [formatter setTimeZone:[NSTimeZone systemTimeZone]];

     [formatter setLocale:[NSLocale currentLocale]];

     

    然而在上传时候更多的使用时间戳标记,以下时间戳与日期的互相转换:

    NSDate *date = [NSDate date];
    NSLog(@"当前日期为:%@",date);
    NSTimeInterval timeStamp= [date timeIntervalSince1970];
    NSLog(@"日期转换为时间戳 %@ = %f", date, timeStamp);//这是以秒为单位
     
    NSString *timeStamp2 = @"1414956901";
    long long int date1 = (long long int)[timeStamp2 intValue];
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:date1];
    NSLog(@"时间戳转日期 %@  = %@", timeStamp2, date2);
     
    原文地址:http://blog.it985.com/8776.html

     

  • 相关阅读:
    从投影的角度理解pca:向量,投影,基,内积,坐标,维数,分散程度,方差,协方差矩阵,对角化,特征值分解,主成分分析PCA
    随机变量的方差variance & 随机向量的协方差矩阵covariance matrix
    对于随机变量的标准差standard deviation、样本标准差sample standard deviation、标准误差standard error的解释
    fmri当前相关软件工具整理
    NPAIRS框架的理解
    phy+
    当前,思路+进展+idea+下一步要做的工作
    dagli最早干了这样一件事儿 Localization of Cardiac-Induced Signal Change in fMRI
    zollei的心动噪声探索性识别
    第九章——中位数和顺序统计量
  • 原文地址:https://www.cnblogs.com/nxmusic/p/nxmusic.html
Copyright © 2011-2022 走看看