如何将字符串转换为日期以及从字符串转换日期?

发布于 2025-01-05 15:42:16 字数 568 浏览 2 评论 0原文

我的输入字符串是

NSString *inputString=@"15 February 2012 17:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd MMMM YYYY HH:mm"];
NSDate *dateVal = [dateFormat dateFromString:inputString];

NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"MM/dd/YYYY HH:mm"];
NSString *outputString = [dateFormat2 stringFromDate:dateVal];
[dateFormat release];
[dateFormat2 release];

我得到以下日期 outputString=12/25/2011 17:05

但应该是02/15/2012 17:05..我在哪里做错了?

My Input String is

NSString *inputString=@"15 February 2012 17:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd MMMM YYYY HH:mm"];
NSDate *dateVal = [dateFormat dateFromString:inputString];

NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"MM/dd/YYYY HH:mm"];
NSString *outputString = [dateFormat2 stringFromDate:dateVal];
[dateFormat release];
[dateFormat2 release];

I got the following date
outputString=12/25/2011 17:05

But it should be 02/15/2012 17:05..Where did i do the mistake?

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

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

发布评论

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

评论(4

山川志 2025-01-12 15:42:16

日期格式不正确,请使用:

[dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"];

大写“YYYY”用于基于“一年中的周”的日历。

请参阅:unicode Date_Format_Patterns

顺便说一句,NSLogdateVal 会表明问题出在输入格式上。

The date format is incorrect, use:

[dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"];

Capital "YYYY" is for use in "Week of Year" based calendars.

See: unicode Date_Format_Patterns

BTW, an NSLog of dateVal would have shown that the problem was on the input formatting.

剑心龙吟 2025-01-12 15:42:16

您使用的日期格式错误,也无需创建 NSDateFormatter 的另一个实例 -

    NSString *inputString=@"15 February 2012 17:05"; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"]; 
    NSDate *dateVal = [dateFormat dateFromString:inputString];

    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"];
    NSString *outputString = [dateFormat stringFromDate:dateVal];

You are using wrong format for date format and also there is no need to create another instance of NSDateFormatter -

    NSString *inputString=@"15 February 2012 17:05"; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"]; 
    NSDate *dateVal = [dateFormat dateFromString:inputString];

    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"];
    NSString *outputString = [dateFormat stringFromDate:dateVal];
世俗缘 2025-01-12 15:42:16

您的区域设置是否与您尝试解析的格式相对应?默认情况下,NSDateFormatter 使用当前区域设置。

Does your locale correspond to the format you are trying to parse? By default NSDateFormatter uses current locale.

入怼 2025-01-12 15:42:16

输入日期格式应为:

 [dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"];

The input date format should be:

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