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

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;

Related

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

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

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

How to see if a date is beween two other dates, and the time used and the time remaining?

If I have set dates like Sunday Jan.29, 2012 2:00:00 PM and Friday Feb.3 2012 5:00:00 PM,
and get the present time, how to I get the spent time from the first date and the present and how do I get the remaining time from the present and the future date?
I have code to show but it is all wrong. There has to be a easy way to do it that I just cant see.
Thank you
Eric
You want to use functions from NSDate
for example:
//get time difference between someDate and now
NSTimeInterval diff = [someDate timeIntervalSinceNow];
//get difference between dates
NSTimeInterval diff2 =[someDate timeIntervalSinceDate: otherDate];
//comparing dates
NSDate * earlierDate = [someDate earlierDate: otherDate];
NSDate * laterDate = [someDate laterDate: otherDate];
If you have your dates available as NSDate objects, you can use timeIntervalSinceDate: to calculate the difference in seconds.
NSTimeInterval sinceThen = [firstDate timeIntervalSinceDate:[NSDate date]];
which will give you the time difference as an NSTimeInterval which is basically a double specifying the time in seconds. If the interval is negative, then firstDate is before now (which is the result of [NSDate date] otherwise it is in the future. If your date is not yet in NSDate form, you might employ an NSDateFormatter to do this (See here, parsing date strings).

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.

In iPhone, how to calculate the passed time?

I want know the passed time that also is between two date,
for example , dateTime1 and dateTime2 , I want a function like getPassedTime( time1, time2)
The function result should be like 1 day 3 hours 12 minutes 10 second or just 40 second.
I know that the method timeIntervalSinceDate of the class NSDate can be use, but the problem is convert to details string that could be easy understand.
Anyone can help? share some code will be appreciate or give me some tip if possible.
Use the timeIntervalSinceDate method of the NSDate class:
NSDate *date1 = [NSDate dateWithString:#"2001-03-24 10:45:32 +0600"];
NSDate *date2 = [NSDate dateWithString:#"2001-03-28 12:38:52 +0600"];
NSTimeInterval *timePassed = [date2 timeIntervalSinceDate:date1];
You could use NSCalendar and NSDateComponents.
Code snippet in this thread.