How to convert NSDate into unix timestamp iphone sdk? - ios

How to convert an NSDate into Unix timestamp? I've read many posts which do the reverse. But I'm not finding anything related to my question.

I believe this is the NSDate's selector you're looking for:
- (NSTimeInterval)timeIntervalSince1970

A Unix timestamp is the number of seconds since 00:00:00 UTC January 1, 1970. It's represented by the type time_t, which is usually a signed 32-bit integer type (long or int).
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.
Since they both have the same reference (midnight 1Jan1970 UTC) and are both in seconds the conversion is easy, convert the NSTimeInterval to a time_t, rounding or truncating depending on your needs:
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];

You can create a unix timestamp date from a date this way:
NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970];

- (void)GetCurrentTimeStamp
{
NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
[objDateformat setDateFormat:#"yyyy-MM-dd"];
NSString *strTime = [objDateformat stringFromDate:[NSDate date]];
NSString *strUTCTime = [self GetUTCDateTimeFromLocalTime:strTime];//You can pass your date but be carefull about your date format of NSDateFormatter.
NSDate *objUTCDate = [objDateformat dateFromString:strUTCTime];
long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);
NSString *strTimeStamp = [Nsstring stringwithformat:#"%lld",milliseconds];
NSLog(#"The Timestamp is = %#",strTimestamp);
}
- (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *objDate = [dateFormatter dateFromString:IN_strLocalTime];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *strDateTime = [dateFormatter stringFromDate:objDate];
return strDateTime;
}
NOTE :- The Timestamp must be in UTC Zone, So I convert our local Time to UTC Time.

Swift
Updated for Swift 3
// current date and time
let someDate = Date()
// time interval since 1970
let myTimeStamp = someDate.timeIntervalSince1970
Notes
timeIntervalSince1970 returns TimeInterval, which is a typealias for Double.
If you wanted to go the other way you could do the following:
let myDate = Date(timeIntervalSince1970: myTimeStamp)

If you want to store these time in a database or send it over the server...best is to use Unix timestamps. Here's a little snippet to get that:
+ (NSTimeInterval)getUTCFormateDate{
NSDateComponents *comps = [[NSCalendar currentCalendar]
components:NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit
fromDate:[NSDate date]];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:[[NSTimeZone systemTimeZone] secondsFromGMT]];
return [[[NSCalendar currentCalendar] dateFromComponents:comps] timeIntervalSince1970];
}

My preferred way is simply:
NSDate.date.timeIntervalSince1970;

As per #kexik's suggestion using the UNIX time function as below :
time_t result = time(NULL);
NSLog([NSString stringWithFormat:#"The current Unix epoch time is %d",(int)result]);
.As per my experience - don't use timeIntervalSince1970 , it gives epoch timestamp - number of seconds you are behind GMT.
There used to be a bug with [[NSDate date]timeIntervalSince1970] , it used to add/subtract time based on the timezone of the phone but it seems to be resolved now.

[NSString stringWithFormat: #"first unixtime is %ld",message,(long)[[NSDate date] timeIntervalSince1970]];
[NSString stringWithFormat: #"second unixtime is %ld",message,[[NSDate date] timeIntervalSince1970]];
[NSString stringWithFormat: #"third unixtime is %.0f",message,[[NSDate date] timeIntervalSince1970]];
first unixtime 1532278070
second unixtime 1532278070.461380
third unixtime 1532278070

If you need time stamp as a string.
time_t result = time(NULL);
NSString *timeStampString = [#(result) stringValue];

Related

NSDate dateWithTimeIntervalSince1970 adding an extra day on

I have a method that updates a label and acts as a stop watch. It works fine accept when I format the string to factor days in it adds an additional day on. For example. If the stopwatch is started ten minutes ago the label will display:
01:00:10:00
it should just display 00:00:10:00
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSDate *dateValue=[[NSUserDefaults standardUserDefaults] objectForKey:#"pickStart"];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:dateValue];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
// Create a date formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd:HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
// Format the elapsed time and set it to the label
NSString *timeString = [dateFormatter stringFromDate:timerDate];
self.stopWatchLabel.text = timeString;
}
Any help would be greatly appreciated.
What you are doing isn't appropriate. Your goal seems to be to convert timeInterval into days, hours, minutes, and seconds. Your use of timerDate and NSDateFormatter are not the proper way to achieve that goal.
timeInterval is not an offset from January 1, 1970 and timeInterval doesn't represent a date.
What you should do is get the difference between currentDate and dateValue as a set of NSDateComponents.
- (void)updateTimer {
NSDate *currentDate = [NSDate date];
NSDate *dateValue = [[NSUserDefaults standardUserDefaults] objectForKey:#"pickStart"];
unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:unitFlags fromDate:dateValue toDate:currentDate options:0];
int days = [comps day];
int hours = [comps hours];
int minutes = [comps minutes];
int seconds = [comps seconds];
NSString *timeString = [NSString stringWithFormat:#"%d:%02d:%02d:%02d", days, hours, minutes, seconds];
self.stopWatchLabel.text = timeString;
}
There is no "additional day", the "date" you produce is a point in time after the 1st Jan 1970, so if your format includes the day you get at least a 1...
Just stick with the NSTimeInterval value and use NSDateComponentsFormatter - which also formats time intervals despite the name - or just do the math yourself to get the seconds, minutes, etc. and format those.
HTH

Format date to secounds

Background: I have dates stored in files.
What I would like to do: I would like to take the difference between two dates in seconds. I can't find any way to do it. My date format looks like that:
2015-23-02-12-23-43
Try this out:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd-hh-mm-ss"];
NSDate *date = [dateFormatter dateFromString:string1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
Try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:EnterYourStringHere];
NSString *str = [dateFormatter stringFromDate:date];
If all you need is seconds then use
NSTimeInterval dateInterval = [date timeIntervalSince1970];
Then
NSString secondsString = [NSString stringWithFormat: #"%.0f", dateInterval];
EDIT:
If you have a file full of date strings that look like your example, 2015-23-02-12-23, then you could use code like this:
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"yyyy-mm-dd-hh-mm-ss"];
//an example - you'll read from your file
NSString *aDateString = #"2015-23-02-12-23";
NSDate aDate = [myFormatter dateFromString: aDateString];
NSTimeInterval dateSeconds = [aDate timeIntervalSince1970];
That will give you dateSeconds as a double precision floating point number, which is the norm for numeric date calculations since it deals with fractions of seconds. You can then do numeric comparisons of date's time intervals.
Note that most UNIX systems use the numeric values returned by the timeIntervalSince1970 method, but Mac and iOS uses numeric date values returned by the method timeIntervalSinceReferenceDate. The two methods have different "epoch dates", or dates where they start counting from zero.
timeIntervalSince1970 uses midnight on Jan 1, 1970 (GMT)
timeIntervalSinceReferenceDate uses midnight on Jan 1, 2001 (GMT)
Which you use is up to you, just be sure to use the same method in all cases.
By the way, your date string format is horrible. having all those 2 digit numbers separated by dashes doesn't give the reader any way to tell apart month/day/hours/minutes/seconds. It's all a jumble. You'd be much better off using a standard date format.
In the US it's common to display dates in the form mm/dd/yyyyy (or yyyy/mm/dd, or even yyyy/dd/mm), and times as hh:mm:ss, so in "mm/dd/yyyyy hh:mm:ss" format you'd get:"09/02/2015 13:09:39" (I'm using human-readable date format strings for discussion, not those intended to set up a date formatter.)

Compare Date object?

I have this date object from Parse, "2015-07-24 20:36:38 +0000" and I would like to compare it to today's date to see how much time has passed since the date on the date object. Any ideas how to do this?
I have this method I use to get the hour and minute, but I'm stuck on how to use this to compare dates outside of the current day.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm"];
NSString *formattedTime = [dateFormatter stringFromDate: date];
You can use an NSTimeInterval for this, the code is:
NSTimeInterval timeInterval = [date timeIntervalSinceNow];
with date being the date from parse as an NSDate, not NSString. and there should be no need to format it before comparing. Hope this helps
If you want this formatted as a string, in iOS 8 and later you can use NSDateComponentsFormatter:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.allowedUnits = NSCalendarUnitMinute | NSCalendarUnitHour;
NSString *string = [formatter stringFromDate:date toDate:[NSDate date]];

Localize the NSDate and Convert timezone of NSDate

In my iPhone / iPad app I am showing a UIDatePicker for time. It will display time in this format, 11:00 AM. When User clicks on the time row we expand the time row to display this datePicker row.
I have a time stamp in string "starts": "11:00", // time of day in ISO-8601 format
I need to show this on the picker wheel as selected time when it gets opened up. For this, first of all I get the date at 12 AM using https://stackoverflow.com/a/9040554/4082792. Then I convert the given time (11:00) to number of seconds and add it to the midnight time to get the current time. This time is in local timezone (as I specify the timezone while using NSDateFormatter). When I try to setDate to UIDatePicker with this date, It gives me incorrect time, even though the time is correct in the NSDate variable. For 11:00 AM, it gives me 6:40 while the local time is 4:30.
So, I have two questions :
1) Why is the time wrong on wheel.
2) How can I convert the NSDate from one timezone to another, I need to show it in the local time format.
Snippet :
NSString *strDate = #"11:00";
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSUInteger preservedComponents = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit);
date = [calendar dateFromComponents:[calendar components:preservedComponents fromDate:date]];
///Start time
NSString *startTime = #"11:00";
NSArray *startTimeSeparatedByColon = [startTime componentsSeparatedByString:#":"]; /// From 22:10 to [22, 10];
NSInteger hourPartOfStart = startTimeSeparatedByColon[0] ? [startTimeSeparatedByColon[0] integerValue] : 0;
NSInteger minutePartOfStart = startTimeSeparatedByColon[1] ? [startTimeSeparatedByColon[1] integerValue] : 0;
NSTimeInterval totalTime = (hourPartOfStart*60*60+minutePartOfStart*60);
NSDate *finalDate = [NSDate dateWithTimeInterval:totalTime sinceDate:date];
NSDate *dt = [NSDate dateWithTimeInterval:[NSTimeZone localTimeZone].secondsFromGMT sinceDate:finalDate];
self.datePicker.date = dt;
By default, iOS converts date into the device's time zone.
But if you want to convert date into another time zone, here is the code for that:
NSTimeZone *currentDateTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:currentDateTimeZone];
You can get the date in "EST" time zone from this dateFormatter object.
Convert "EDT" TimeZone
NSString *format = #"EEEE dd-MMMM-yyyy HH:mm:ss z";
NSDateFormatter *date_EDTDateFormate = [[NSDateFormatter alloc] init];
date_EDTDateFormate setDateFormat:format];
date_EDTDateFormate setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"EDT"]];
NSString *stringEDT = [dateFormatter_EDT stringFromDate:date_System];

How to get iOS date format of current date into an integer?

I am trying to figure out how to get the current date and time into a number up to the seconds?
I saw this post What format string do I use for milliseconds in date strings on iPhone? but I am actually trying to get it into just a number...
EDIT: Sorry meant seconds
if you want UTC timestamp and want to store in int than on 32 bit this is not possible use instead long long which has larger range and can be used as integer
NSString *dateValue = #"2011-06-23T13:13:00.000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString *formatString = #"yyyy-MM-dd'T'HH:mm:ss.SSS";
[formatter setDateFormat:formatString];
NSDate *date = [formatter dateFromString:dateValue];
long long dateInMillis = [date timeIntervalSince1970]*1000; //This will give milliseconds
long dateInSeconds = [date timeIntervalSince1970]; //This will give seconds
EDIT: For current date in long value use
//This will give seconds of current date
long dateInSeconds = [[NSDate date] timeIntervalSince1970];
You can convert an NSDate to a double using:
NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970];
millis:
long long milliseconds = llround(([[NSDate date] timeIntervalSince1970] * 1000.0));

Resources