Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone OS: How do I create an NSDate for a specific date?

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.

like image 339
nickthedude Avatar asked Apr 05 '10 17:04

nickthedude


People also ask

What is NS date?

Overview. NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

How do I get today's date in Objective C?

Get Today's Date:NSDate* date = [NSDate date];

How does Objective C compare to NSDate?

There are 4 methods for comparing NSDate s in Objective-C: - (BOOL)isEqualToDate:(NSDate *)anotherDate. - (NSDate *)earlierDate:(NSDate *)anotherDate. - (NSDate *)laterDate:(NSDate *)anotherDate.


2 Answers

@Chuck's answer is correct, and lead me to the following code. Thought I'd share:

NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:10]; [comps setMonth:10]; [comps setYear:2010]; NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps]; 
like image 134
Oded Ben Dov Avatar answered Oct 07 '22 15:10

Oded Ben Dov


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.

like image 26
Kamleshwar Avatar answered Oct 07 '22 16:10

Kamleshwar