How to convert any human readable date to unix timestamp [duplicate] - iphone

This question already has answers here:
How to convert NSDate into unix timestamp iphone sdk?
(10 answers)
Closed 9 years ago.
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.

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).

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];

Related

How to convert unix timestamp to human readable time?

I get a unix timestamp from the database and I am trying to create a human readable date from it. I am using this way
long t1=[time longLongValue];
NSDate* date=[NSDate dateWithTimeIntervalSince1970:t1];
where time is the timestamp. When I print date I get
1956-02-18 19:04:01 +0000
instead of
2013-01-02 12:31:03 +0000
The timestamp was 1356765933449
It is a matter of integer overflow, as Boris correctly pointed out in his answer.
I don't know what your time object is, but instead of a signed long int use a NSTimeInterval.
On iOS NSTimeInterval is currently defined as
typedef double NSTimeInterval;
but you shouldn't care too much about that. Sticking with type synonyms will protect you in case Apple decides to change the underlying definition to something else.
That said you should change your code to something like
NSTimeInterval epoch = [time doubleValue];
NSDate * date = [NSDate dateWithTimeIntervalSince1970:epoch];
Concerning the code maintainability issue I described before, here you are explicitly using a doubleValue (you don't have many options), but the good thing is that if Apple changes the NSTimeInterval definition to something not compatible with a double assignment, the compiler will let you know.
Try this
- (NSString *) getDateFromUnixFormat:(NSString *)unixFormat
{
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[unixFormat intValue]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMM dd, yyyy-h:mm"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
//NSDate *date = [dateFormatter dateFromString:publicationDate];
NSString *dte=[dateFormatter stringFromDate:date];
[dateFormatter release];
return dte;
}
The Unix timestamp has only 32 Bits available.
Because they use a signed int, they count the seconds from 1.1.1970. A 32 Bit signed int can only hold values up to 2147483647, where as you want it to be 1356765933449. That causes an overflow, and that causes your date to be invalid.
This is also known as the Year 2038 Problem, because 2147483647 (max value) will be hit on 03:14:07 UTC on Tuesday, 19 January 2038.
Then format the date using nsdateformatter. Details guide.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

calculate epoch time in iPad

I have an array which contains time in HH:mm:ss format.
I want to convert this into epoch time. To convert a particular time into epoch, date along with time should be passed.
I want to pass today's date along with the time from array.
For example 09:15:30 (date 22/12/2011) which is a string, should be converted into 1324525530000 (corresponding epoch value)
How should I convert this ??
Any help is appreciated.
Maybe you can get an idea looking at this snippet:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] initWithSafeDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSTimeInterval epoch = [date timeIntervalSince1970];
Anyway you can have a look to NSDateFormatter and NSDate in the reference docs.
That looks like an epoch time in milliseconds - as far as I know it's supposed to be in seconds.
You can use NSDateFormatter to parse a string and convert it to a NSDate. This date can then return the seconds since 1970. If you really need it, simply multiply this by 1000.

WCF DateTime to NSDate

I am using a web services which returns a JSON WCF DateTime.
The string goes like \/Date(1316397792913+0800)\/
I am interested in extracting out the 1316397792 which is the time since 1st Jan 1970 in seconds. So that I can use the NSDate timeIntervalSince1970 method to get the present time. I cropped out the last 3 digits as it's in milliseconds and the timeIntervalSince1970 takes in seconds.
Here's what I am currently doing which does not work for dates somewhere before 2001 which has a time interval in ms since 1970 less than 10 characters.
NSString *dateString = #"\/Date(1316397792913+0800)\/";
NSLog(#"dateString :%#", dateString);
NSDate *date = [NSDate dateWithTimeIntervalSince1970:
[[dateString substringWithRange:NSMakeRange(6, 10)] intValue]];
NSLog(#"NSDate:%#", date);
dateString :/Date(1316397792913+0800)/
NSDate:2011-09-19 02:03:12 +0000
Therefore, I need a better work around playing with the dateString which does not assume everytime we will be feteching 10 characters.
You can simply get the substring between the index of ( and the index of +. Something like:
NSRange begin = [dateString rangeOfString:"("];
NSRange end = [dateString rangeOfString:"+"];
NSString* milliSecondsSince1970 = [dateString substringWithRange:NSMakeRange(begin.location + 1, end.location - begin.location - 1)];
P.S: Check for the one off error.

Best way to store a 'time' value

My class needs two properties: startTime and endTime. What is the best class to use? I know there is NSDate, but I only need to store a specific time (something in between 00:00-23:59), I don't need a date. What is the most elegant solution here?
NSTimeInterval is probably good enough for this.
It stores a time value in seconds as a double.
Eg. 5 mins = 300.0
I believe the most elegant solution, and what you want, is NSTimeInterval, that is the primitive type that NSDate is built on top.
NSTimeInterval is a typedef for double, and is a measurement of time in seconds. This primitive time type do not have any concept of a reference date. What NSDate do is to add this concept of reference date and anchor the 0.0 time at 1 January 2001 GMT. There is nothing that stops you from inventing your own reference date or anchor, like for example "midnight of whatever day there is".
What you can do is to add two properties of the NSTimeInterval either as startTime and endTime and let them both use midnight as the reference. Or you could skip endTime and go for a startTime and duration combo.
There's NSDateComponents, which "can also be used to specify a duration of time, for example, 5 hours and 16 minutes."
The NSDate class is similar to the DateTime class in C#: both hold a date and time, but they can be independent of each other. In Cocoa, you would compare two NSDate classes:
//Create NSDate objects in the time format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *startTimeString = #"00:00:00"; //0 seconds
NSString *endTimeString = #"00:00:52"; //52 seconds
NSDate *startTime = [dateFormatter dateFromString:startTimeString];
NSDate *endTime = [dateFormatter dateFromString:endTimeString];
//Compare the time
BOOL date1before2 = [startTime compare:endTime] == NSOrderedAscending;

How to convert NSDate into Unix timestamp in Objective C/iPhone? [duplicate]

This question already has answers here:
How to convert NSDate into unix timestamp iphone sdk?
(10 answers)
Closed 8 years ago.
How can I convert an NSDate to a Unix timestamp in Objective-C?
I believe - [NSDate timeIntervalSince1970] is what you want. Jan 1, 1970 is the date of the Unix epoch.
NSDate *date = [NSDate date];
NSTimeInterval ti = [date timeIntervalSince1970];
NSTimeInterval is typedefed as a double, defined in seconds. If you want it in integer form, just cast the result to a long and use that instead, but this gives more precision.
I like properties.
NSDate.date.timeIntervalSince1970;