//获得当前时间并且转为字符串
- (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]*;//客户端当前13位毫秒级时间戳
NSString *timeSp = [NSString stringWithFormat:@"%llu",recordTime];//时间戳转字符串(13位毫秒级时间戳字符串)
return timeSp;
}
 //时间戳字符串1469193006001(毫秒)1469193006.001(毫秒,1469193006001234(微秒)1469193006.001234(微秒)转 UTC时间2016-08-11T07:00:55.611Z
- (NSString *)timespToUTCFormat:(NSString *)timesp
{
NSString *timeString = [timesp stringByReplacingOccurrencesOfString:@"." withString:@""];
if (timeString.length >= ) {
NSString *second = [timeString substringToIndex:];
NSString *milliscond = [timeString substringFromIndex:];
NSString * timeStampString = [NSString stringWithFormat:@"%@.%@",second,milliscond];
NSTimeInterval _interval=[timeStampString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *dateString = [dateFormatter stringFromDate:date]; return dateString;
}
return @"";
}

//13位时间戳1469193006001(毫秒)转 系统时间2016-08-11 08:55:36
 + (NSString *)timespToYMDFormat:(NSNumber *)timesp
{
NSString *stime = [timesp stringValue];
NSTimeInterval time = [[stime substringToIndex:] doubleValue];
NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; return [dateFormatter stringFromDate: detaildate];
}


//            时间转时间戳的方法:sendDate为NSDate类
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[sendDate timeIntervalSince1970]];

//12-5-11添加

如果只获取当前的年月日,用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);
 

iOS 本地时间、UTC时间、时间戳等操作、获取当前年月日的更多相关文章

  1. iOS 本地时间 / UTC时间 / 时间戳等操作 / 获取当前年月日

    //获得当前时间并且转为字符串 - (NSString *)dateTransformToTimeString { NSDate *currentDate = [NSDate date];//获得当前 ...

  2. 获取当前时间UTC时间的下一个15分钟时间点

    ZonedDateTime zdt = ZonedDateTime.now(ZoneOffset.UTC); int now15Minute = zdt.getMinute() / P15MINUTE ...

  3. mysql 日期操作 增减天数、时间转换、时间戳(转)

    转自http://www.cnblogs.com/wenzichiqingwa/archive/2013/03/05/2944485.html http://hi.baidu.com/juntao_l ...

  4. mysql 日期操作 增减天数、时间转换、时间戳(转换)

    http://hi.baidu.com/juntao_li/item/094d78c6ce1aa060f6c95d0b MySQL datediff(date1,date2):两个日期相减 date1 ...

  5. java UTC时间和local时间相互转换

    java UTC时间和local时间相互转换 1.local时间转UTC时间 /** * local时间转换成UTC时间 * @param localTime * @return */ public ...

  6. C#实现UTC时间与Datetime转换

    为了便于传输,通信过程中传输的都是:当前时间跟标准时间相隔的秒数,并且是以16进制字节的形式传输的. public double ConvertDateTimeInt(System.DateTime ...

  7. C# 获取utc时间,以及utc datetime 互相转化

    C# 获取utc时间,以及utc datetime 互相转化   大部分源于http://blog.sina.com.cn/s/blog_4c6e822d0102dsdz.html 刚开始学习一点C# ...

  8. MySQL 获得当前日期时间(以及时间的转换)

    1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp()   curr ...

  9. [转帖]UTC时间、GMT时间、本地时间、Unix时间戳

    UTC时间.GMT时间.本地时间.Unix时间戳 https://www.cnblogs.com/xwdreamer/p/8761825.html 引用: https://blog.csdn.net/ ...

随机推荐

  1. Git自动补全

    一.简介 假使你使用命令行工具运行Git命令,那么每次手动输入各种命令是一件很令人厌烦的事情.为了解决这个问题,你可以启用Git的自动补全功能,完成这项工作仅需要几分钟.   二.操作步骤 1) cd ...

  2. [模板]RMQ(冲刺准备中)

    洛谷P3865 注意:位运算一定要加括号!因为他的优先级没有加减法高: 注意在预处理的时候判断的是前一个区间是否完整,故 i+(1<<(j-1))-1<=n; 取logn时最好多加一 ...

  3. [Jenkins] 执行SoapUI的task,里面包含多个Project,发出的报告也要求包含多个Project,设置邮件内容为HTML+CSS

    执行SoapUI的task,里面包含多个Project,发出的报告也要求包含多个Project,设置邮件内容为HTML+CSS 如何保证样式在邮件内容中不丢失 <link title=" ...

  4. ceph常用指令

    一.集群 1.启动一个ceph 进程 启动mon进程 service ceph start  mon.node1 启动msd进程 service ceph start mds.node1 启动osd进 ...

  5. 【转】Java虚拟机详解----常用JVM配置参数

    原文地址:http://www.cnblogs.com/smyhvae/p/4736162.html 本文主要内容: Trace跟踪参数 堆的分配参数 栈的分配参数 零.在IDE的后台打印GC日志: ...

  6. ScreenCapturePro2 for Joomla_3.4.7-tinymce4x

    1.1. 与Joomla_3.4.7-tinymce4x整合 示例下载:Joomla_3.4.7,   1.1.1. 添加screencapture文件夹   1.1.2. 2.添加插件文件夹 路径: ...

  7. 支持stl容器的gdb自定义命令

    # 本文可以从https://sourceware.org/ml/gdb/2008-02/msg00064/stl-views.gdb直接下载 # 有关gdb的高级使用,请浏览:http://blog ...

  8. Java反射api详解

    虽然已经用了很多反射相关的api,但是还没有详细研究官方api文档,下面以1.6为基础研究一下api文档(因为只找到了1.6的中文版...),同时补充1.7和1.8对反射相关api的改变.

  9. [js]利用闭包向post回调函数传参数

    最近在闲逛校园XX站的时候,打算搞个破坏,试试有多少人还是用初始密码登陆.比较懒,所以直接打开控制台来写. 所以问题可以描述为: 向后端不断的post数据,id从1~5000自增,后端会根据情况来返回 ...

  10. Hadoop 基础概念

    Hadoop就是一个实现了Google云计算系统的开源系统,包括并行计算模型Map/Reduce,分布式文件系统HDFS,以及分布式数据库Hbase,同时Hadoop的相关项目也很丰富,包括ZooKe ...