Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer to a date string

In Objective-C: Is there a simple way, as in Excel and SAS, to convert an integer into a datetime string?

In Excel, you'd format to "mmm dd, yyyy hh:mm:ss".

For instance, if I had the integer: 1328062560

and I want the output to be: 2012-01-31 21:16:00

or even (as in Excel): Jan 31, 2012 21:16:00

PS: I don't want to be too demanding, but I'd like it simple and inline for use in NSLog, so I can write simply for debugging
NSLog("The time as an integer is %d and as a date %mmmddyyyyhh:mm:ss", [array objectAtIndex: i], [array objectAtIndex: i]);

like image 330
Peter_from_NYC Avatar asked Feb 01 '12 02:02

Peter_from_NYC


People also ask

Is date a string or integer?

A calendar date is stored internally as an integer value equal to the number of days since December 31, 1899. Because DATE values are stored as integers, you can use them in arithmetic expressions. For example, you can subtract a DATE value from another DATE value.

Can we convert integer to date in SQL?

The method will convert the integer value into dd/MM/yyyy format by extracting dd, MM, and yyyy parts separately using arithmetic operation and add / between MM and yyyy parts. It is then converted into VARCHAR datatype. CONVERT function with style 103 is used to convert the formatted string into a proper date value.


1 Answers

You can get an NSDate object like this:

NSDate *date = [NSDate dateWithTimeIntervalSince1970:1328062560];

Then, if you don't care about the exact format of the string, you can just do this:

NSString *s = [date description];

That will give you a string like “2012-02-01 02:16:00 +0000”.

If you want a specific string format, use an NSDateFormatter.

like image 197
rob mayoff Avatar answered Sep 30 '22 05:09

rob mayoff