栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 中时间与时间戳的转换实例详解

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 中时间与时间戳的转换实例详解

C++ 中时间与时间戳的转换实例详解

//  设置时间显示格式:

  NSString *timeStr = @"2011-01-26 17:40:50";

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

  [formatter setDateStyle:NSDateFormatterMediumStyle];

  [formatter setTimeStyle:NSDateFormatterShortStyle];

  [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

  //设置时区,这个对于时间的处理有时很重要

  //例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.

  //例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?

  //他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了

  NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];

  [formatter setTimeZone:timeZone];

  NSDate *date = [formatter dateFromString:timeStr]; //------------将字符串按formatter转成nsdate

  NSLog(@"date = %@", date);

  NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式

  NSLog(@"datenow = %@", datenow);

  NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------将nsdate按formatter格式转成nsstring,nsstring会显示与当前的时间吻合的串

  NSLog(@"nowtimeStr = %@", nowtimeStr);

//  时间转时间戳的方法:

  NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];

  NSLog(@"timeSp:%@",timeSp); //时间戳的值

//  时间戳转时间的方法

  NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1296035591];

  NSLog(@"1296035591 = %@",confromTimesp);

  NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

  NSLog(@"confromTimespStr = %@",confromTimespStr);

//  时间戳转时间的方法:

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

  [formatter1 setDateStyle:NSDateFormatterMediumStyle];

  [formatter1 setTimeStyle:NSDateFormatterShortStyle];

  [formatter1 setDateFormat:@"yyyyMMddHHMMss"];

  NSDate *date1 = [formatter1 dateFromString:@"1283376197"];

  NSLog(@"date1:%@",date1);

 

当前时间是:14:41:57

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/63535.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号