2012-07-17 70 views
1

我们正在构建一款应用程序,需要在设备上存储日期才能进入该应用程序,该应用程序将具有国际性,因此我们遇到了两个困境/挑战。IOS NSDateFormatter问题

  1. 用户首

    如果用户选择我们从[NSDate的日期]这样2012-07-17上午11时26分03秒的日期返回的12小时rahter超过24小时格式这对于在SQLite数据库中排序并不理想,因为我们无法将其存储为日期。

  2. 用户区域

    通常,这是好的但是在这里blighty我们有一个叫做英国夏季一wonderfult事情。每10月25日至30日在一个周期内增加一个小时,并在一个周期内每3月25日至31日移除一个小时,所以如果一年中的8个月没有进行调整,则时间会落后一个小时。

我需要实现的是此格式一致的日期:2012-07-17 11点26分03秒无论身在何处设备所在,也考虑到其中GMT + 1进入的地方。

任何帮助都会很棒。

编辑 *

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; 
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT+01:00"]; 
[dateFormatter setTimeZone:gmt]; 
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
NSDate *localDate = [dateFormatter dateFromString:timeStamp]; 


NSLocale* currentLocale = [NSLocale currentLocale]; 
NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode]; 
NSLog(@"Country Code: %@", countryCode); 
NSLog(@"Current Loacle: %@", currentLocale); 
if(([countryCode isEqualToString: @"GB"])){ 
    NSDate *mydate = [NSDate date]; 
    NSDate *fiddlyFoo = [mydate dateByAddingTimeInterval:3600]; 
    NSLog(@"Print GMT +1 %@",fiddlyFoo); 
} else { 
    NSLog(@"Print GMT Local %@",localDate); 
} 

回答

4

我现在做这样的事情。请注意,NSDate“知道”当前时区和夏令时等。因此,您只需要以可排序的表示形式获取GMT时间版本。我建议RFC 3339,但你可以根据你的需要使用就可以了变化:

此代码:

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 

// Create a local date for London, for testing purposes 
NSDateComponents *comps = [NSDateComponents new]; 
[comps setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]]; 
[comps setDay:1]; 
[comps setMonth:7]; 
[comps setYear:2012]; 
[comps setHour:14]; 
NSDate *date = [gregorian dateFromComponents:comps]; 

// Just want to show this date is 2PM in London July 1st 
NSDateFormatter *curFormat = [NSDateFormatter new]; 
[curFormat setDateStyle:NSDateFormatterFullStyle]; 
[curFormat setTimeStyle:NSDateFormatterFullStyle]; 
NSLog(@"Reference date is good no: %@", [curFormat stringFromDate:date]); 

// So now we get the date as a rfc3339 string, referenced to GMT 
NSString *timeString; 
{ 
    NSDateFormatter *rfc3339 = [NSDateFormatter new]; 
    [rfc3339 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 
    rfc3339.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; 
    timeString = [rfc3339 stringFromDate:date]; 
} 
// referenced to UTC (sortable with any other time), can save in SQL DB etc 
NSLog(@"Date as rfc3339 string: %@", timeString); 

// Now lets convert it back into a BST time 
NSDate *newDate; 
{ 
    NSDateFormatter *rfc3339 = [NSDateFormatter new]; 
    [rfc3339 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; 
    rfc3339.timeZone = [NSTimeZone timeZoneWithName:@"UTC"]; 
    newDate = [rfc3339 dateFromString:timeString]; 

    // we want to show this as a string 2012-07-17 11:26:03 
    NSDateFormatter *newFormatter = [NSDateFormatter new]; 
    [newFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // local time 
    [newFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/London"]]; 

    NSLog(@"Local string using 24 hour clock: %@", [newFormatter stringFromDate:newDate]); 
} 

生成的输出,我相信这是你想要什么:

TimeTester[58558:f803] Reference date is good no: Sunday, July 1, 2012 9:00:00 AM Eastern Daylight Time 
TimeTester[58558:f803] Date as rfc3339 string: 2012-07-01T13:00:00Z 
TimeTester[58558:f803] Local string using 24 hour clock: 2012-07-01 14:00:00 
+0

谢谢但是,当我将字符串转换回日期时,我再次获得AM/PM附录,我需要停止。我们需要这个作为NSDate,因为我们在应用程序的其他地方正在做一些东西 – 2012-07-17 12:04:47

+0

那么,上面的代码给你一个表示,你可以排序,哪些是基于GMT的。如果您需要将其转换为想要呈现给用户的字符串,那么会出现一个反向编码,它会给您一个NSDate - 是您想要的吗? – 2012-07-17 12:50:14

+0

我很抱歉,我的目标只是为了获得24小时日期格式,无论时区或区域设置如何,但对BST有调整 – 2012-07-17 12:55:11