我是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. 如何从00:00到当前时间获取今日时间?
- 2. Kendo UI DateTimePicker - 从00:30到24:00显示30分钟的时间间隔
- 3. 如何获得系统日期与时间00:00:00
- 4. MySQL CSV导入 - 如果时间戳记为毫秒,则输入日期为0000-00-00 00:00:00?
- 5. 将chr“00:00:00”转换为日期时间“00:00:00”
- 6. SQL日期时间的更新时间为00:00:00值
- 7. 选择时间戳为00:00:00
- 8. 转换C#日期时间为0000-00-00 00:00:00到MySQL
- 9. 选择是的日期时间列是0000-00-00 00:00:00
- 10. 检测/解析00:00:00(不含日期)到日期时间
- 11. CSV导入,空白时间戳导致0000-00-00 00:00:00
- 12. 在Sql中将时间格式从00:00:00更改为秒
- 13. 从时间跨度(例如“15分钟”或“2秒”)到“00:15:00”或“00:00:02”
- 14. Javascript时间08:30 - 20:00
- 15. VB 2008转换字符串 “08:30:00”,以日期时间 “08:30:00”
- 16. 字符串到日期 - 在C#中[\t“Mon Jan 13 2014 00:00:00 GMT + 0000(GMT标准时间)”]
- 17. 时间戳YYYY-MM-DDTHH:MM:SS + 00:00
- 18. jQuery的 - 格式分钟和秒 - 00:00
- 19. 如何将时间戳记设置回到0000-00-00 00:00:00?
- 20. 如果日期时间为0000-00-00 00:00:00,则更改为NOW();?
- 21. 向上舍入时间和日期C# - 当时间= 00:00:00时增加日期为1
- 22. Android中的日期,时间,分钟和秒数获取日期和时间差异
- 23. 如何从5分钟汇总数据,到30分钟,同时保持日期的时间间隔等数值
- 24. 获取时间为00:00:00.0以下
- 25. 在22:30和1:00之间的时间戳LINQ查询
- 26. 无效的日期/时间字符串:Sun Jun 03 2012 00:00:00 GMT + 0100(GMT Daylight Time)CFML
- 27. 小时到分钟00:00到00 javascript
- 28. 从时间戳获取日期
- 29. 如何在oracle和java中保留24小时00分钟的时间戳值
- 30. SQL查询中的修改日期字段从00:00 GMT到12:00 GMT
你想改变,我认为这应该有助于区域: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