NSDateFormatter 确实返回了预期的结果

发布于 2024-11-17 07:49:40 字数 866 浏览 3 评论 0 原文

我创建了一个从 NSString 返回 NSDate 的方法。代码如下。我发送到函数“2/25/2011”。该函数返回“2010-12-26 08:00:00 +0000”

我做错了什么?最后我还需要释放 NSString 。我该怎么做?

谢谢你!

-(NSDate *) GetDatefromString:(NSString *) Str
{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"MM-dd-YYYY"];

    NSDate *dateFromString;
            // = [[NSDate alloc] init]; tried taking this part out but still not working


    if (Str != NULL)
    {
        dateFromString = [formatter dateFromString:Str];

    }
    else
    {
        dateFromString = [formatter dateFromString:@"1/1/1970"];
    }

    [formatter release];

     return dateFromString;    

// how do I release dateFromString?

  /*  
    Printing description of dateFromString:
    2010-12-26 08:00:00 +0000
    Printing description of Str:
    02-25-2011
  */  
}

I created a method to return the NSDate from a NSString. Code is below. I send to the function "2/25/2011". The function returns "2010-12-26 08:00:00 +0000"

What did I do wrong? Also the NSString I need to release at the end. How do I do that?

thank you!

-(NSDate *) GetDatefromString:(NSString *) Str
{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"MM-dd-YYYY"];

    NSDate *dateFromString;
            // = [[NSDate alloc] init]; tried taking this part out but still not working


    if (Str != NULL)
    {
        dateFromString = [formatter dateFromString:Str];

    }
    else
    {
        dateFromString = [formatter dateFromString:@"1/1/1970"];
    }

    [formatter release];

     return dateFromString;    

// how do I release dateFromString?

  /*  
    Printing description of dateFromString:
    2010-12-26 08:00:00 +0000
    Printing description of Str:
    02-25-2011
  */  
}

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

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

发布评论

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

评论(3

自控 2024-11-24 07:49:40

您使用 / 分隔符传递日期,但指定 - 作为日期格式化程序的分隔符。至于关于释放字符串的问题,您应该只释放您拥有的对象,即您使用 newalloccopy 创建的对象,或mutableCopy。请参阅下面的示例。

NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; // needs releasing
[formatter setDateFormat:@"MM-dd-yyyy"];

NSString* str1 = @"06/25/2011"; // does not need releasing
NSString* str2 = [[NSString alloc] initWithString:@"06-25-2011"]; // needs releasing

NSDate* date1 =  [formatter dateFromString:str1]; // does not need releasing
NSDate* date2 =  [[NSDate alloc] init]; // needs releasing
date2 = [formatter dateFromString:str2]; 

NSLog(@"date from %@ : %@", str1, date1);
NSLog(@"date from %@ : %@", str2, date2);

// release the objects you own
[formatter release];
[str2 release];
[date2 release];

// prints out
date from 06/25/2011 : (null)
date from 06-25-2011 : 2011-06-25 00:00:00 -0700

You are passing in a date using the / seperator but you specified - as a separator for your date formatter. As to your question on releasing the string, you should only release objects that you own, i.e. that you have created with new, alloc, copy, or mutableCopy. See examples below.

NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; // needs releasing
[formatter setDateFormat:@"MM-dd-yyyy"];

NSString* str1 = @"06/25/2011"; // does not need releasing
NSString* str2 = [[NSString alloc] initWithString:@"06-25-2011"]; // needs releasing

NSDate* date1 =  [formatter dateFromString:str1]; // does not need releasing
NSDate* date2 =  [[NSDate alloc] init]; // needs releasing
date2 = [formatter dateFromString:str2]; 

NSLog(@"date from %@ : %@", str1, date1);
NSLog(@"date from %@ : %@", str2, date2);

// release the objects you own
[formatter release];
[str2 release];
[date2 release];

// prints out
date from 06/25/2011 : (null)
date from 06-25-2011 : 2011-06-25 00:00:00 -0700
不交电费瞎发啥光 2024-11-24 07:49:40

您将格式设置为@"MM-dd-YYYY",但传入的字符串的格式为@"1/1/1970"。那不匹配。请参阅 数据格式化指南:日期格式化程序


// how do I release dateFromString?

使用 自动释放

return [dateFromString autorelease];

请参阅内存管理

you set the format to@"MM-dd-YYYY"but the string you pass in has the form @"1/1/1970". That doesn't match. see Data Formatting Guide: Date formatters.


// how do I release dateFromString?

With autorelease.

return [dateFromString autorelease];

See Memory management.

毁梦 2024-11-24 07:49:40

我遇到的问题是格式中有年份的大写字母。

这是正确的:

 [formatter setDateFormat:@"MM/dd/yyyy"];

这是错误的: 注意大写字母 YYYY

[formatter
setDateFormat:@"MM-dd-YYYY"];

The problem I had was the format had CAP LETTERS for the YEAR.

This is correct:

 [formatter setDateFormat:@"MM/dd/yyyy"];

And this is wrong: NOTE THE CAPS YYYY

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