0

I'm new to iPhone development. I want to set default date to NSDate Object as string. I don't see any easy way or method...

I think there might be a method in NSCalender? If there's such a method, please tell me.

Thanks in advance.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
Ankit
  • 1

5 Answers5

0
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; //here,you can set the date format as you need

NSDate *now = [[[NSDate alloc] init]autorelease];
NSString *theDate = [DateFormatter stringFromDate:now];

Now, you can use the string the date. :)

0

Edit: Added time to format string.

You will need to look at the NSDate and NSDateFormatter classes. Here's a simple example of how to use them:

NSString* defaultDateString = [NSString stringWithFormat:@"2011-01-22 15:30:00"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate* defaultDate = [dateFormatter dateFromString:defaultDateString];
[dateFormatter release];

and if you wanted to get the string from a date you can just use:

NSString* defaultDateString = [dateFormatter stringFromDate:defaultDate];
David
  • 7,310
  • 6
  • 41
  • 63
  • yup buddy i forgot sudenly i get answer from my own code.......BUT thank you very much for give me time – Ankit Jan 26 '11 at 07:00
0

I'm not totally clear on what you are asking, but to create an instance of an NSDate object with the current date, one calls: NSDate * myDate = [NSDate date];

If you are saying that you have a c-string or NSString that needs to be parsed to initialize an NSDate object, that's another question.

I have some code posted here: How get a datetime column in SQLite with Objective C

that shows how to create NSDates from NSStrings using NSDateFormatter.

Community
  • 1
  • 1
xyzzycoder
  • 1,831
  • 13
  • 19
0

If you want to create an NSDate from a string, you need to use an NSDateFormatter to do it. It's important to note that the formatter will use the current locale's time zone when constructing the date, unless you put a time-zone in as part of the format. For more information about constructing time zones, see NSTimeZone.

For example, to create a date using the ubiquitous format '2011-01-16 00:00' in UTC, you would do:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
// Only certain abbreviations are okay, like UTC. See docs for more info
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSDate* midnight_26_jan_2011_utc = [formatter dateFromString:@"2011-01-26 00:00"];

// this will display in your system locale
// (for me, it shows 2011-01-25 19:00 +0500 because I'm America/New_York time)
NSLog(@"date: %@", midnight_26_jan_2011_utc);
[formatter release];
Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • hi jason thank you for answer but i have problem in taking 24hr time.. if i take 13:00 than it not work properly in my Counter so help me – Ankit Jan 26 '11 at 08:38
  • @Ankit you can use 12 hour time too, or no time at all. If you don't care about the time part, don't put the HH:mm in there; if you want 12-hour time you can use hh:mm instead. – Jason Coco Jan 26 '11 at 18:03
-2
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:@"yyyy-mm-dd"];
NSDate *yourDate = [dateFormatter dateFromString:@"2011-01-26"];
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
Ankit
  • 1
  • Four spaces indentation is all you need for a code block. Also, welcome to the site! – Rafe Kettler Jan 26 '11 at 07:01
  • 'mm' is the specifier for minutes, not months. So your example /looks/ like it works because January is the default month, but this actually creates a date of 26 Jan 2011 at 00:01 hours local time. If your date was say '2011-10-01', the created date would be 1 /Jan/ 2011 at 00:10 local O_O Be careful! – Jason Coco Jan 26 '11 at 07:06
  • ok it`s fine but it not take 24 hR time for that what can i do? – Ankit Jan 26 '11 at 08:37