ios iPhone OS:如何为特定日期创建 NSDate?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2579738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone OS: How do I create an NSDate for a specific date?
提问by nickthedude
Seems like a simple thing but I can't seem to find a way to do it.
似乎是一件简单的事情,但我似乎无法找到一种方法来做到这一点。
It would be great to see a couple different methods.
很高兴看到几种不同的方法。
回答by Oded Ben Dov
@Chuck's answer is correct, and lead me to the following code. Thought I'd share:
@Chuck 的回答是正确的,并引导我到以下代码。以为我会分享:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:10];
[comps setMonth:10];
[comps setYear:2010];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps];
回答by Kamleshwar
You can use this
你可以用这个
NSString *dateString = @"03-Sep-11";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd-MMM-yy";
NSDate *date = [dateFormatter dateFromString:dateString];
Hope this will help you out.
希望这会帮助你。
回答by Chuck
If you're talking about a specific calendar date rather than a UNIXy date, you probably want NSCalendar's dateFromComponents:
.
如果您谈论的是特定的日历日期而不是 UNIXy 日期,您可能需要 NSCalendar 的dateFromComponents:
.
回答by Suragch
Swift Examples
Swift 示例
Examples are often the easiest way to learn. Here are a few examples.
示例通常是最简单的学习方法。这里有一些例子。
Now
现在
This one is the easiest.
这个是最简单的。
let currentDateTime = Date()
February 20, 2017
2017 年 2 月 20 日
// Specify date components
var dateComponents = DateComponents()
dateComponents.year = 2017
dateComponents.month = 2
dateComponents.day = 20
// Create date from components
let userCalendar = Calendar.current // user calendar
let dateThisPostWasUpdated = userCalendar.date(from: dateComponents)
April 1, 1976
1976 年 4 月 1 日
// Specify date components
var dateComponents = DateComponents()
dateComponents.year = 1976
dateComponents.month = 4
dateComponents.day = 1
// Create date from components
let userCalendar = Calendar.current // user calendar
let appleFoundedDate = userCalendar.date(from: dateComponents)
Need more details...?
需要更多细节...?
There are other ways to create dates, too. If you want to learn those, as well as how to displaya date, then see my fuller answer.
还有其他方法可以创建日期。如果您想了解这些以及如何显示日期,请参阅我的更完整的答案。
回答by zoul
See the documentation for NSDate. You can use the dateFromString:
method of NSDateFormatter
or the dateFromComponents:
method of NSCalendar
.
请参阅NSDate的文档。您可以使用 的dateFromString:
方法NSDateFormatter
或 的dateFromComponents:
方法NSCalendar
。
回答by Lev Chlumov
I found this thread while looking for an answer to this question but then I found a good example in one my Big Nerd Ranch Objective-C Programming book. Credit to them, not me.
我在寻找这个问题的答案时发现了这个线程,但后来我在我的 Big Nerd Ranch Objective-C 编程书中找到了一个很好的例子。归功于他们,而不是我。
NSDateComponents *myBDay =[[NSDateComponents alloc] init];
[myBDay setDay:22];
[myBDay setMonth: 04];
[myBDay setYear: 1984];
[myBDay setHour:9];
[myBDay setMinute:25];
[myBDay setSecond:35];
NSCalendar *g = [[ NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *dateOfBirth = [g dateFromComponents:myBDay];
回答by Mike Gledhill
It is ridiculous, isn't it ?
这很荒谬,不是吗?
Mid-2013 and Apple stillhasn't provided a simple way to set a NSDate value.
2013 年年中,Apple仍然没有提供设置 NSDate 值的简单方法。
During my current iPad project, I couldn't believe I had to stop productivity for a while to write my own helper class to get the Year value from an NSDate. I mean, come on, this is basic stuff.
在我当前的 iPad 项目中,我无法相信我不得不停止生产力一段时间来编写我自己的帮助类来从 NSDate 中获取 Year 值。我的意思是,来吧,这是基本的东西。
Anyway, here's the helper class I used in my project, to convert a string into an NSDate value :
无论如何,这是我在项目中使用的帮助类,用于将字符串转换为 NSDate 值:
@implementation DateHelper
+(NSDate*)parseDateString:(NSString *)dateString
{
NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
[rfc3339TimestampFormatterWithTimeZone setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[rfc3339TimestampFormatterWithTimeZone setDateFormat:@"MMM dd, yyyy"];
NSDate *theDate = nil;
NSError *error = nil;
if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
}
return theDate;
}
@end
Using this code, you could set an NSDate value using something like:
使用此代码,您可以使用以下内容设置 NSDate 值:
NSDate* date = [DateHelper parseDateString:@"Jul 16, 2013"];
Note: this function was based on code taken from here: https://stackoverflow.com/a/3968411/391605
注意:此功能基于取自此处的代码:https: //stackoverflow.com/a/3968411/391605
My solution had beento use the following code, but I found that sometimes, it just wouldn't parse, and would return nil.
我的解决方案是使用以下代码,但我发现有时,它不会解析,并且会返回nil。
// Take a date string in the format "Oct 23, 2013", and convert it into a NSDate value
// THIS DOESN'T WORK ! DON'T TRUST THIS CODE !!
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM dd, yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate* date = [formatter dateFromString:dateString];
I remember it failed miserably on "Oct 12, 2012"... which is why I gave up and used the more complicated "parseDateString" function shown above.
我记得它在“2012 年 10 月 12 日”惨败……这就是我放弃并使用上面显示的更复杂的“parseDateString”函数的原因。
My point is... be careful.
我的意思是……小心点。
Some of the very-basic NSDate functions just don't work properly...
一些非常基本的 NSDate 函数无法正常工作......