简体   繁体   English

我如何从此字符串获取NSDate,并将此NSDate转换为long或NSTimeInterval

[英]How can i get a NSDate from this string, and this NSDate to long or NSTimeInterval

I have a string date "Mar+5,+2014+4:00:00+AM"and I tried convert NSDate, and next get long(NSTimeInterval): 我有一个字符串日期“ Mar + 5,+ 2014 + 4:00:00 + AM”,我尝试转换NSDate,然后变长了(NSTimeInterval):

expireDate = @"Mar+5,+2014+4:00:00+AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM+dd,+yyyy+h:mm:ss+a"];
NSDate *dte = [dateFormat dateFromString:expireDate];

NSLog(@"%f", [dte timeIntervalSince1970]);

But, show me result: 0000.0. 但是,请显示结果:0000.0。 What the problem? 什么问题 Please help me. 请帮我。 I need this date in long for store in db. 我需要这个日期很长时间才能存储在数据库中。

可能需要设置区域设置,以独立于用户的设置:

[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

It looks like your code works, possibly change the format to %.0f to only show seconds. 看起来您的代码可以正常工作,可能将格式更改为%.0f以仅显示秒。
Added an additional NSLog of the date for debugging. 添加了用于调试日期的其他NSLog

NSString *expireDate = @"Mar+5,+2014+4:00:00+AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM+dd,+yyyy+h:mm:ss+a"];
NSDate *dte = [dateFormat dateFromString:expireDate];
NSLog(@"%@", dte);
NSLog(@"%.0f", [dte timeIntervalSince1970]);

NSLog output for my timezone (EST): 我的时区(EST)的NSLog输出:

2014-03-05 09:00:00 +0000 2014-03-05 09:00:00 +0000
1394010000 1394010000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM