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]);
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With