简体   繁体   English

无法从字符串获取NSDate

[英]Not getting NSDate from string

I have following date in string formate "date string : 2014-09-28 17:30:00" 我在字符串格式“日期字符串:2014-09-28 17:30:00”中具有以下日期

Now, I want to convert it into NSDate. 现在,我想将其转换为NSDate。

I have use following code for this. 我为此使用了以下代码。

NSString *date = @"2014-09-28 17:30:00";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter1 dateFromString:date];
NSLog(@"date from string: %@", dateFromString);

I got the following output. 我得到以下输出。

date from string: 2014-09-28 12:00:00 +0000

So, Here Time is changed. 因此,此处时间已更改。

Please tell me, How can I convert into NSDate. 请告诉我,如何转换成NSDate。 What I am missing here? 我在这里想念的是什么?

When you pass an NSDate object into NSLog , it will print the NSDate in GMT time, which may not be your timezone (and why you were getting 12:00 instead of 17:30), this would also cause the output of your NSLog statement to be different for people who are running your code in different timezones, so what you want to do is call the [NSDateFormatter stringFromDate:] method if you want to keep your specified time from your date object: 当您将NSDate对象传递给NSLog ,它将以格林尼治标准时间显示NSDate ,这可能不是您的时区(以及为什么您是12:00而不是17:30),这也会导致NSLog语句的输出对于在不同时区运行您的代码的人来说是不同的,因此,如果要保留日期对象中的指定时间, [NSDateFormatter stringFromDate:]调用[NSDateFormatter stringFromDate:]方法:

So replace this line of code: 因此,替换以下代码行:

// Will print out GMT time by default (+0000)
NSLog(@"date from string: %@", dateFromString);

With this line: 用这行:

// Will honor the timezone of your original NSDate object:
NSLog(@"date from string: %@", [dateFormatter1 stringFromDate:dateFromString]);

And that should print out the value you were hoping for. 这应该可以打印出您想要的值。

// --------------------------// // -------------------------- //

Note: It is important to understand that NSDate objects do not have any concept of timezones, so it is up to the developer to manage and track their timezones with the provided platform methods. 注意:必须了解NSDate对象没有任何时区概念,因此开发人员必须使用提供的平台方法来管理和跟踪其时区,这一点很重要。

On iOS, you can look into using this class: 在iOS上,您可以研究使用此类:

NSTimeZone , which can help you manage/assign your timezone(s) on iOS platforms. NSTimeZone ,可以帮助您在iOS平台上管理/分配时区。

If you are developing for OSX, you can assign a timezone and locale with this method: -descriptionWithCalendarFormat:timeZone:locale: . 如果您正在开发OSX,则可以使用以下方法分配时区和语言环境: -descriptionWithCalendarFormat:timeZone:locale:。 (Sadly, this method is OSX-only) (遗憾的是,此方法仅适用于OSX)

Hope that helps. 希望能有所帮助。

There is a time zone component attached to your log at the end that means the date is converted to the specified time zone..here greenwhich mean time 最后在日志中附加了一个时区组件,这意味着日期已转换为指定的时区。此处绿色表示时间

So converting into your time locale can give you the right value you inserted 因此,转换为您的时间区域设置可以为您提供正确的值

In your first string add +0000 to the end and check again you can see the value is the same you get.ie the conversion is done on the GMT format 在您的第一个字符串中,在末尾添加+0000并再次检查,您可以看到该值与您获得的值相同。即,转换是按GMT格式完成的

visit https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html 请访问https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

This documentation provides you how to convert NSString to date. 本文档为您提供了如何将NSString转换为日期。

this code is provided by apple documentation. 此代码由Apple文档提供。

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];

NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);
// Output for locale en_US: "formattedDateString: Jan 2, 2001".

To make string as date 将字符串作为日期

NSString *formattedDateString = [dateFormatter stringFromDate:date];

stringFromDate: Returns a string representation of a given date formatted using the receiver's current settings. stringFromDate:返回使用接收者当前设置格式化的给定日期的字符串表示形式。

what you had used dateFromString: Returns a date representation of a given string interpreted using the receiver's current settings. 您使用过的dateFromString:返回使用接收者当前设置解释的给定字符串的日期表示形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM