将“2011-11-04T18:30:49Z”转换为至“MM/dd/yy hh:mm:SS a”格式

发布于 2024-12-13 21:53:25 字数 311 浏览 1 评论 0原文

我需要将日期从以下格式转换为:“2011-11-04T18:30:49Z” 转换

为以下格式:“MM/dd/yy hh:mm:SS a”

添加通过以下方式提取的用户系统 currentGMTOffset 偏移量:

NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];  
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];

Any帮助将不胜感激。

谢谢!

I need to convert date from this format: "2011-11-04T18:30:49Z"

To this format: "MM/dd/yy hh:mm:SS a"

With the addition of the users system currentGMTOffset offset extracted by:

NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];  
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];

Any help will be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴ 2024-12-20 21:53:25

此代码片段将帮助您将格式化字符串转换为 NSDate。

    // Current date.
    NSString *currentDate = @"2011-11-04T18:30:49Z";

    // NSDateFormatter stuff.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

    // Convert to NSDate.
    NSDate *neededDate = [dateFormatter dateFromString:currentDate];

然后您只需转换为您要求的格式即可。

我使用 ARC,因此不需要内存管理。

This snippet is going to help you to convert your formated string to a NSDate.

    // Current date.
    NSString *currentDate = @"2011-11-04T18:30:49Z";

    // NSDateFormatter stuff.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

    // Convert to NSDate.
    NSDate *neededDate = [dateFormatter dateFromString:currentDate];

Then you just need to convert to your requested format.

Im using ARC so no memory management is required.

新雨望断虹 2024-12-20 21:53:25

看一下 NSDateFormatter。您需要在日期格式化程序对象上设置日期格式和时区,然后可以将字符串转换为日期,将日期转换为字符串。

Take a look at NSDateFormatter. You'll need to setup a date format and timezone on the date formatter object, and then you can convert strings to dates and dates to strings.

沩ん囻菔务 2024-12-20 21:53:25
+ (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
 Returns a user-visible date time string that corresponds to the specified
 RFC 3339 date time string. Note that this does not handle all possible
 RFC 3339 date time strings, just one of the most common styles.
 */

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);

    [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;

可能会有所帮助。

+ (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
 Returns a user-visible date time string that corresponds to the specified
 RFC 3339 date time string. Note that this does not handle all possible
 RFC 3339 date time strings, just one of the most common styles.
 */

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);

    [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;

}

This might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文