1

I have a date like this 01/20/2013 and trying to get the unix timestamp for this date. I have searched a lot and found out that how to convert present date into unix timestamp but didn't find my solution.

here is my code what i'm doing.

NSDate *date = mydate;     //myDate is the date like 01/20/2013

NSDateFormatter *formatter = [[ NSDateFormatter alloc]init];

[formatter setDateFormat:@"mm/dd/yyyy"];

NSString *timestamp = [formatter stringFromDate:date];

NSLog(@"%@",timestamp);

I'm getting null as timestamp value in my console.

Marc
  • 16,170
  • 20
  • 76
  • 119
Zac24
  • 2,514
  • 1
  • 23
  • 28

2 Answers2

2

iOS provides -(NSTimeInterval)timeIntervalSince1970 for NSDate objects which returns the number of seconds since 00:00:00 GMT January 1, 1970. NSTimeInterval is a double floating point type so you get the seconds and fractions of a second.

time_t unixTime = (time_t) [your_date timeIntervalSince1970];

Here time_t is usually a signed 32-bit integer type (long or int).

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
  • I tried like this but it's crashing. NSDate *date = [[appDelegate.configFileArray objectAtIndex:3] valueForKey:@"1"]; NSLog(@"%@",date); time_t unixTime = (time_t) [date timeIntervalSince1970]; NSLog(@"%ld",unixTime); – Zac24 Apr 30 '13 at 12:52
  • No it should not crash. I have checked this. Ex: time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970]; NSLog(@"unixtime : %li",unixTime); It returns me unixtime : 1367326434 – Nishant Tyagi Apr 30 '13 at 12:54
  • I think you are passing wrong date. Please check it once. – Nishant Tyagi Apr 30 '13 at 12:55
  • NSDate *date = [[appDelegate.configFileArray objectAtIndex:3] valueForKey:@"1"]; NSLog(@"%@",date); when i do this then in my log m getting "01/20/2013". is this wrong date to pass. – Zac24 Apr 30 '13 at 12:58
  • What is the date can you tell in your console ? – Nishant Tyagi Apr 30 '13 at 13:00
  • in my console i'm getting like this "01/20/2013". – Zac24 Apr 30 '13 at 13:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29189/discussion-between-zac24-and-nishant-tyagi) – Zac24 Apr 30 '13 at 13:07
1

you can get it with

- (NSTimeInterval)timeIntervalSince1970

UPDATE:

For Example in int ...

int unixtimestamp = [mydate timeIntervalSince1970];

And other Example in NSTimeInterval

NSTimeInterval ti = [mydate timeIntervalSince1970];
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • this will convert the present date to unix timestamp.. how can i provide my own date..? – Zac24 Apr 30 '13 at 12:54
  • you have string Date now?? – Paras Joshi Apr 30 '13 at 12:55
  • @Zac24 i update answer dear see that... if you have string date then use my method for convert that String date to NSDate... – Paras Joshi Apr 30 '13 at 12:56
  • @Zac24 just pass that date in that like NSDate *date = [[appDelegate.configFileArray objectAtIndex:3] valueForKey:@"1"];int unixDateInt = [date timeIntervalSince1970]; – Paras Joshi Apr 30 '13 at 13:01
  • i tried that but it crashing. i think some problem with date i'm passing as Nishant said above but i dont know what's the problem n how to fix it. – Zac24 Apr 30 '13 at 13:07
  • first check that you get value in [[appDelegate.configFileArray objectAtIndex:3] valueForKey:@"1"]?? – Paras Joshi Apr 30 '13 at 13:08
  • @Zac24 its crashed when you date variable have no value and its `nil`... that is the reason i am sure.. – Paras Joshi Apr 30 '13 at 13:09
  • ya i logged it and in my console m getting this "01/20/2013". – Zac24 Apr 30 '13 at 13:09
  • now i use this code.. `int unixtimestamp = [[NSDate date] timeIntervalSince1970]; NSLog(@"\n\n =>>> Int==>> %d",unixtimestamp);` and it logs **=>>> Int==>> 1367327533** – Paras Joshi Apr 30 '13 at 13:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29191/discussion-between-zac24-and-paras-joshi) – Zac24 Apr 30 '13 at 13:19