Timestamp C

I have found many timestamp transformations, and they all work, but the fact is that when I put my code in a date text form, I always go 4 months ahead.

It pulls the current day of the week, date and time. I have it installed this way, because I select the day using the DateTime Picker. This is just my viewDidLoad to pull today's date.

NSDate *myDate = [[NSDate alloc] init]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"cccc, MMMM dd, YYYY, hh:mm aa"]; NSString *prettyVersion = [dateFormat stringFromDate:myDate]; date.text = prettyVersion; 

Now there is a time stamp conversion to accept prettyVersion in timeIntervalSince1970

 NSDate *startdates = [dateFormat dateFromString:prettyVersion]; NSLog(@"Timestamp %0.0f",[startdates timeIntervalSince1970]); 

NSLog displays "Timestamp 1356317580", which when converted - Mon, 24 Dec 2012 02:53:00 GMT

Now i can do it

  NSLog(@"Timestamp2 %0.0f",[myDate timeIntervalSince1970]); 

and get the correct timestamp.

So where did I mess around.

+8
unix-timestamp ios objective-c
source share
1 answer

This question has already been answered: NSDateFormatter dateFromString gives an invalid date

But the gist of this is in your dateFormat date, you should use "yyyy" instead of "YYYY".

From the apple documentation :

A common mistake is to use YYYY. yyyy indicates the calendar year, while YYYY indicates the year ("Week of the year") used in the calendar year ISO year. In most cases, yyyy and YYYY give the same number, but they can be different. Usually you should use a calendar year.

 [dateFormat setDateFormat:@"cccc, MMMM dd, yyyy, hh:mm aa"]; 

And then for me it will be formatted until "Sunday, September 15, 2013, 07:24 PM", which was when I ran this for testing.

+7
source share

All Articles