我是iOS(Objective-c)编码的新手,我被困在时间戳上。 我得到时间戳,而JSON解析即。 2017-04-30T14:30+00:00(GMT)
。如何从这个时间戳得到日期,小时,分钟和秒?我在GMT
中获得这种格式,是否有可能将其转换为"IST"
?怎么样?如何从时间戳中获取日期,时间,分钟和秒数“2017-04-30T14:30 + 00:00(GMT)”?
回答
日期格式模式
甲日期模式是一个字符,其中的字符特定字符串是从日历格式化时替换为日期和时间数据,或用于解析时为日历生成数据串。以下是模式中用于显示给定语言环境的适当格式的字符。下面举例说明:
- (NSString *)curentDateStringFromDate:(NSDate *)dateTimeInLine withFormat:(NSString *)dateFormat {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:dateFormat];
NSString *convertedString = [formatter stringFromDate:dateTimeInLine];
return convertedString;
}
使用它象下面这样:
NSString *dateString = [self curentDateStringFromDate:[NSDate date] withFormat:@"dd-MM-yyyy"];
NSString *timeString = [self curentDateStringFromDate:[NSDate date] withFormat:@"hh:mm:ss"];
NSString *hoursString = [self curentDateStringFromDate:[NSDate date] withFormat:@"h"];
在Foundation framework
,涉及用于此任务的使用(在任一方向上)的类是NSDateFormatter
Refer here
下面的代码将GMT转换为IST。
NSString *inDateStr = @"2000/01/02 03:04:05";
NSString *s = @"yyyy/MM/dd HH:mm:ss";
// about input date(GMT)
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init];
inDateFormatter.dateFormat = s;
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSDate *inDate = [inDateFormatter dateFromString:inDateStr];
// about output date(IST)
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init];
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"];
outDateFormatter.dateFormat = s;
NSString *outDateStr = [outDateFormatter stringFromDate:inDate];
// final output
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr);
thanx帮助@ Suresh.D兄弟。格林尼治标准时间到IST完成谢谢你的帮助。但我的主要问题仍然是如何从该时间戳中获取日期,小时数。如果我能得到很大帮助的帮助。 –
@MaulikDesai更新了我的答案,请去检查,如果你有答案,请接受答案。提前致谢。 –
这帮助我感谢你的帮助。 –
- 1. 将chr“00:00:00”转换为日期时间“00:00:00”
- 2. 选择时间戳为00:00:00
- 3. 在Sql中将时间格式从00:00:00更改为秒
- 4. 从时间跨度(例如“15分钟”或“2秒”)到“00:15:00”或“00:00:02”
- 5. Javascript时间08:30 - 20:00
- 6. VB 2008转换字符串 “08:30:00”,以日期时间 “08:30:00”
- 7. 如果日期时间为0000-00-00 00:00:00,则更改为NOW();?
- 8. 如何将时间戳记设置回到0000-00-00 00:00:00?
- 9. 在22:30和1:00之间的时间戳LINQ查询
- 10. 如何在Python中将日期时间字符串中的时间从24:00转换为00:00?
- 11. 如何在oracle和java中保留24小时00分钟的时间戳值
- 12. 在MySQL数据库中插入日期和时间的日期时间列时,它将插入日期但时间00-00-00
- 13. SQL查询中的修改日期字段从00:00 GMT到12:00 GMT
- 14. 日期格式在JavaScript与时间00:00:00
- 15. 日期(现在)只能使用时间00:00:00
- 16. 如何在PHP中以这种格式生成日期时间戳“2013-11-16T12:30:00 + 02:00”?
- 17. 不正确的日期时间值“0000-00-00 00:00:00” - DATE_SUB()在具有
- 18. 无法更新日期时间字段,值变为0000:00:00 00:00:00
- 19. 插入空的日期/时间到MySQL与PHP导致0000-00-00/00:00:00
- 20. 如何从Unix纪元时间毫秒获取GMT日期?
- 21. 熊猫:不能对日期时间索引与这些索引器[2016-08-01 00:00:00]的时间戳
- 22. 我需要从22:00:00到02:30:00之间从mysql获取记录
- 23. 如何更新它为NULL或'0000-00-00'的日期时间?
- 24. 如何检查日期和时间戳是20分钟之前
- 25. 通过pd.TimeGrouper('D')按日汇总小时时间系列;问题@时间戳00:00:00(24小时)
- 26. 日期时间从7天现在在00:00
- 27. 从时间戳分开获取时间和日期
- 28. MySQL查询选择分钟日期时间30天的间隔
- 29. 使用T-SQL将日期时间转换为“2013-02-04T10:30:23 + 00:00”
- 30. 我想环路两个时间之间,从8:00到17:00,每15分钟
你想改变,我认为这应该有助于区域:http://stackoverflow.com/a/7259553/6203030 http://stackoverflow.com/questions/13138957/convert-date-in-mm -dd-yyyy -forma-in-xcode –
谢谢@AitorPagán的建议。这有助于转换时区。 –
收藏本页:http://nsdateformatter.com – diatrevolo