3

NSDateFormatter を使用して、保存された日付を山岳標準時間に変更しようとしていますが、機能していないようです。

「currentDate: 2013-02-22 23:20:20 +0000 date With date formatter: other 2000-01-01 07:00:00 +0000」をコンソールに出力しています。

文字列が正しく作成されていないようですが、通常と同じ方法で呼び出しているようです。

提案?

    NSTimeZone * mtnTimeZ= [NSTimeZone timeZoneWithAbbreviation:@"MST"];

    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setTimeZone:mtnTimeZ];

    NSString * timeZ = [dateFormatter stringFromDate:currentDate];

    NSDate * newDate = [dateFormatter dateFromString:timeZ];
    NSLog(@"currentDate: %@ string With dateFormatter: %@ date made from string %@", currentDate, timeZ, newDate);
4

2 に答える 2

1

次の行を入れてみてください。

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

このような:

NSTimeZone * mtnTimeZ= [NSTimeZone timeZoneWithAbbreviation:@"MST"];

NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
[dateFormatter setTimeZone:mtnTimeZ];

NSString * timeZ = [dateFormatter stringFromDate:currentDate];

NSDate * newDate = [dateFormatter dateFromString:timeZ];
NSLog(@"currentDate: %@ string With dateFormatter: %@ date made from string %@", currentDate, timeZ, newDate);
于 2013-02-22T23:44:18.977 に答える