//获得当前时间并且转为字符串
- (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. windows 查看端口被占用进程

    查看占用63243是谁 C:\Users\Administrator>netstat -aon|findstr "63243" TCP 172.27.33.11:63243 ...

  2. 2PC之JTA原理与实现

    转自:https://www.ibm.com/developerworks/cn/java/j-lo-jta/index.html 利用 JTA 处理事务 什么是事务处理 事务是计算机应用中不可或缺的 ...

  3. 商业级别Fortify白盒神器介绍与使用分析

    转自:http://www.freebuf.com/sectool/95683.html 什么是fortify它又能干些什么? 答:fottify全名叫:Fortify SCA ,是HP的产品 ,是一 ...

  4. Boost智能指针使用总结

    内存管理是一个比较繁琐的问题,C++中有两个实现方案: 垃圾回收机制和智能指针.垃圾回收机制因为性能等原因不被C++的大佬们推崇, 而智能指针被认为是解决C++内存问题的最优方案. 1. 智能指针定义 ...

  5. sql分组获取第一条记录(sql+oracle)

    sql版本 select * from (select t.CloseDate,t.ExpiryDate,t.DataTypeLookupID,ROW_NUMBER() over(partition ...

  6. win10 startup启动目录路径命令

    仅对当前用户生效: C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 开始-运行 shel ...

  7. PHP json_encode中文unicode转码问题

    用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式,如果想汉字不进行转码,这里提供三种方法 1.升级PHP,在PHP5.4, 这个问题终于得 ...

  8. centos7用fdisk进行分区

    1.查看分区信息:fdisk -l 从上面可以看到,/dev/sdb分区还没有使用,现在将其划分成2个10G的分区. 2.执行:fdisk /dev/sdb 1):fdisk命令参数 p:打印分区表. ...

  9. HDU 2159 FATE (二维背包)

    题意:中文题. 析:dp[i][j] 已经杀了 i 个怪兽,已经用了 j 体积,所能获得的最大经验值,这个和一维的差不多,只是加一维而已. 代码如下: #pragma comment(linker, ...

  10. VC6.0 如何显示代码行号

    VC6.0是一款比较经典.稳定的功能强大的IDE,目前也有很多人在使用.但美中不足的是它不能像其他IDE那样显示行号.这里需要用到一个插件VC6LineNumberAddin, 下载地址:http:/ ...