如何在 iPhone 中将 nsstring 更改为 nsdate 格式?

发布于 2024-12-01 14:03:46 字数 632 浏览 2 评论 0原文

我一直在努力将 nsstring 转换为 nsdate 格式。我有一个 nsstring 格式的日期,例如 (2011-08-25 18:30:00 +0000),但我想将其更改为 nsdate 格式 (08/26/2011)。如何改变呢?有人可以帮我吗?提前致谢...我花了一天的时间来解决这个问题,但我找不到解决方案..请帮助我的朋友...

这是我的代码,

    NSString *dateString = @"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"Converted date is %@",dateFromString);

输出是,

         Converted date is (null)

Yuva.M

I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....

This is my code,

    NSString *dateString = @"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"Converted date is %@",dateFromString);

Output is,

         Converted date is (null)

Yuva.M

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

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

发布评论

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

评论(3

热情消退 2024-12-08 14:03:46
NSString *dateWithInitialFormat = @"2011-08-25 18:30:00 +0000";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];

[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);

来自 NSLog:dateWithNewFormat:08/25/2011

请参阅日期格式模式

NSString *dateWithInitialFormat = @"2011-08-25 18:30:00 +0000";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];

[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);

From NSLog: dateWithNewFormat: 08/25/2011

See Date Format Patterns

风轻花落早 2024-12-08 14:03:46

您需要使用 NSDateFormatter 并设置日期格式以匹配您的存储方式,如下所示。

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];

You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
云巢 2024-12-08 14:03:46
NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MMMM dd, yyyy"];
    NSDate* d = [df dateFromString:@"January 06, 1992"];
    NSLog(@"%@", d);

    NSDate *dispalyDate = d;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd MMM"];
    NSString *str = [dateFormat stringFromDate:dispalyDate];
    NSLog(@"%@", str);
NSDateFormatter* df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MMMM dd, yyyy"];
    NSDate* d = [df dateFromString:@"January 06, 1992"];
    NSLog(@"%@", d);

    NSDate *dispalyDate = d;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd MMM"];
    NSString *str = [dateFormat stringFromDate:dispalyDate];
    NSLog(@"%@", str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文