从stange看字符串的NSDate [英] NSDate from stange looking string
问题描述
我有一个NSDictionary,其日期值为:
日期:/日期(1314313200000 + 0100)/,
如何将其转换为NSDate,因为它包含字符串谎言日期:和/?
I have an NSDictionary with the value for date as:
"Date":"/Date(1314313200000+0100)/",
How can I turn this into an NSDate as it contains strings lie "Date": and / ?
推荐答案
1314313200000是自纪元日期以来的毫秒数(1970-01-01) ,0100是时区。您需要从字符串中解析此信息并从中构建日期。 NSScanner
类非常适合从格式错误的文本中解析信息。
1314313200000 is milliseconds since the epoch-date (1970-01-01), and 0100 is the timezone. You need to parse this info out from the string and build a date from it. The NSScanner
class is a good fit for parsing information out of strangely formatted text.
// Init a scanner with your date string
NSScanner* scanner = [NSScanner scannerWithString:@"Date:/Date(1314313200000+0100)/"];
// Skip everything up to a valid numeric character
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet]
intoString:NULL];
// Parse numeric value into a 64bit variable
long long milliseconds = 0;
[scanner scanLongLong:&milliseconds];
// Create a date instance from milliseonds sinve 1970-01-01
NSDate* date = [NSDate dateWithTimeIntervalSince1970:milliseconds / 1000.0];
如果时区也很重要,请跳过 +
签名并解析一个新号码。
If the time-zone is also important just skip the +
sign and parse a new number.
这篇关于从stange看字符串的NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!