无法从 NSString 获取 NSDate

发布于 2024-11-14 08:21:12 字数 1116 浏览 3 评论 0原文

我可以使用以下代码获取表示 PST 中当前日期和时间的 NSString。

//get current date and time
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"America/Los_Angeles"];    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss zzz"];
    [dateFormatter setTimeZone:timeZone];
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [enUSPOSIXLocale release];
    NSDate *date = [NSDate date];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    NSLog(@"currentDateStr %@",currentDateStr);
    [dateFormatter release];

编辑:

但是以下从 NSString 获取 NSDate 的代码不符合我的目的。

//get date from string
    NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MM/dd/yy HH:mm:ss"];
    NSDate *dt = [[NSDate alloc]init];
    dt = [formatter2 dateFromString:currentDateStr];
    NSLog(@"dt %@",dt);

“dt”似乎为空。

有人可以帮我解决从 NSString 获取 NSDate 的问题吗? 提前致谢。

I am able to fetch NSString representing the current date and time in PST using the following code.

//get current date and time
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"America/Los_Angeles"];    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yy HH:mm:ss zzz"];
    [dateFormatter setTimeZone:timeZone];
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [enUSPOSIXLocale release];
    NSDate *date = [NSDate date];
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    NSLog(@"currentDateStr %@",currentDateStr);
    [dateFormatter release];

EDIT:

But the following code to fetch NSDate from NSString doesn't serve my purpose.

//get date from string
    NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MM/dd/yy HH:mm:ss"];
    NSDate *dt = [[NSDate alloc]init];
    dt = [formatter2 dateFromString:currentDateStr];
    NSLog(@"dt %@",dt);

"dt" appears to be null.

Could anybody help me out in solving this issue of getting NSDate from NSString.
Thanx in advance.

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

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

发布评论

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

评论(3

2024-11-21 08:21:12

NSDateFormatter 还具有 dateFromString: 函数。

编辑:

//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];

[formatter2 setDateFormat:@"MM/dd/yy HH:mm:ss zzz"];

NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(@"dt %@",dt);

NSDateFormatter also has dateFromString: function.

EDIT:

//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];

[formatter2 setDateFormat:@"MM/dd/yy HH:mm:ss zzz"];

NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(@"dt %@",dt);
胡大本事 2024-11-21 08:21:12

利用

[dateFormatter dateFromString:urString];

在您的情况下 urStringcurrentDateStr

Make use of

[dateFormatter dateFromString:urString];

In your case urString is currentDateStr

惯饮孤独 2024-11-21 08:21:12

使用

 - (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];

NSLog(@"I am inside the datePickerValueChanged   - - %@", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}   

Use

 - (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];

NSLog(@"I am inside the datePickerValueChanged   - - %@", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文