Converting Date and time into number of seconds since 1970 - objective-c

I have the date string "2016-04-01T05:00:00+08:00" and I want to convert it into number of seconds since 1 January 1970. I am doing like this:
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [parsingFormatter dateFromString:"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
But in the above example startTime returns nil. Can someone tell me what I am doing wrong? How can I convert it?

Try this
NSDateFormatter *parsingFormatter = [NSDateFormatter new];
[parsingFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [parsingFormatter dateFromString:#"2016-04-01T05:00:00+08:00"];
NSTimeInterval startTime = [date timeIntervalSince1970];
I am getting date "2016-03-31 21:00:00 +0000" UTC format.

Related

NSDateFormatter show midnight as 24 instead of 00

in my application I use NSDate and NSDateformater to show the hour in the following format : HH:mm. When I pass a timestamp that represents midnight for example: 1603843200000
I expect to get the hour as 00:00, instead I get 24:00. How can I get the hour as 00:00?
Here's the code I use to format the timestamp:
NSNumber* timeStamp = currentConsumption.timestamp; // 1603843200000
NSDate* date = [NSDate dateWithTimeIntervalSince1970:([timeStamp longLongValue]/1000)];
NSDateFormatter* df = [NSDateFormatter new];
df.dateFormat = self->graphDateFormat; // #"k:mm"
if(self->graphType == day){
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
}
return [df stringFromDate:date];
The problem was that I set the format to be k:mm instead HH:mm.

Converting timestamp into NSDate

I have a date in timestamp which looks something like this: 1474914600000
Now, I want to covert this timestamp to NSDate in format of dd-mm-yyyy.
How can this be done in objective c?
You need to convert your timestamp to NSDate and then get NSDate in your desired format. Also your timestamp seems to be in millisecond so you will have to divide it be 1000. You can use below code:
double timeStamp = 1474914600000;
NSTimeInterval timeInterval=timeStamp/1000;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString=[dateformatter stringFromDate:date];
dateString value as an output will be: "27-09-2016"
Hope it helps.
To elaborate on balkaran's answer incase you're new to the iOS world. The timestamp you provided seems to go down to milliseconds which you wouldn't need for day times that's why he's dividing by 1000. You would use the dateformatter as follows to return an NSString you can use with the formatted date.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1474914600000];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"dd-MM-yyyy";
NSString *formattedDate = [formatter stringFromDate:date];
use this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
then convert your date in the format you want.

NSDate is wrong when doing 'dateFromString'

Im here in the UK and when working with dates in iOS they are always out by one hour (one hour behind), what I need is the correct time from an NSDate. Ive done the following, but i get two different times:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"NSDate %#",today);
NSLog(#"Time %#", [dateFormatter stringFromDate:today]);
NSDate*stringDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:today]];
NSLog(#"Time date %#",stringDate);
Here is what is logged:
NSDate 2015-07-01 16:07:22 +0000
Time 2015-07-01 17:07:22
Time date 2015-07-01 16:07:22 +0000
Why is this happening? Am i missing something obvious? Surely if the string date is correct, then doing dateFromString should yield the correct results?
The reason I need an NSDate is so I can get the correct amount of seconds using [myTime timeIntervalSince1970]
2015-07-01 17:07:22 //is the correct date
Im expecting an NSDate object that is correct to my date and time.
Update
The answers below helped me find where I was going wrong, so I changed my approach, I was able to get the current timestamp doing the following:
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSDate *curdate = [dateFormatter dateFromString:timeStamp];
int unix_timestamp = [curdate timeIntervalSince1970];
NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone systemTimeZone];
int offset = (int)[timeZone secondsFromGMTForDate: referenceDate];
int currentTimestamp = unix_timestamp + offset;
NSLog(#"CUrrent time stamp %d",currentTimestamp);
NSDate is an absolute moment of time, it does not have a timezone. The date object you have is correct: it is exactly the moment that code was executed.
If you need string representation of that moment of time in a specific time zone, use stringFromDate: just like you did.
If you need to know number values of hour/minute in a specific time zone, use -[NSCalendar components:fromDate:].
[calendar setTimeZone:...];
NSDateComponents* components = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:date];
The date formatter defaults to the local timezone. If you want a different timezone specify it. NSLog of a date used the 'NSDatedescription` method that defaults to GMT (UTC).
Examining the code:
NSDate *today = [NSDate date];
// Creates today's data in GMT (UTC) All NSDates are referenced to GMT.
NSLog(#"NSDate %#",today); (moved up for explanation ordering)
// NSDate 2015-07-01 16:07:22 +0000
// Displays the date in GMT
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
// Creates a date for matter with the system timezone
NSLog(#"Time %#", [dateFormatter stringFromDate:today]);
// Time 2015-07-01 17:07:22
// Creates a string representation in the system timezone and displays it
NSDate*stringDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:today]];
// Creates a date from the string taking into the system timezone
NSLog(#"Time date %#",stringDate);
// Time date 2015-07-01 16:07:22 +0000
// Displays the date in GMT.

How to fix the variance between milliseconds and date conversion?

I am trying to convert a date to milliseconds then from milliseconds to date in objective c. But variance occurs. I had used the following code.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.dateFormat = #"MMM dd, yyyy HH:mm:ss a";
NSDate *dateHolder = [dateFormat dateFromString: #"Nov 25, 2014 4:11:50 PM"];
NSTimeInterval seconds = [dateHolder timeIntervalSince1970];
double milliseconds = seconds*1000;
NSLog(#"Date milliseconds %f", milliseconds);
NSDate *tr = [NSDate dateWithTimeIntervalSince1970: (milliseconds/1000)];
NSLog(#"Date by milliseconds%#", tr);
Output is received as follows
Date milliseconds 1416897710000.000000
Date by milliseconds 2014-11-25 06:41:50 +0000 // Hours and minutes were varied
How to fix ? Thanks in advance, for any help.
My output
Date milliseconds 1416913910000.000000
Date by milliseconds2014-11-25 11:11:50 +0000
Maybe because of the Time Difference ?

converting timestamp to nsdate format

I want to convert my timestamp value 1308031456 to NSDate format (which yields the value Tue, 14 Jun 2011 06:04:16 GMT in online web conversion). How would I convert between the two programmatically?
OBJ - C:
Use dateWithTimeIntervalSince1970:,
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];
SWIFT:
Use init(timeIntervalSince1970:)
let date = NSDate(timeIntervalSince1970:timeStamp)
You can use the use the method dateWithTimeIntervalSince1970: to convert the timestamp you've which is the epoch time.
NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:1308031456];
In obj c
double timeStamp = 1513330403393;
NSTimeInterval unixTimeStamp = timeStamp / 1000.0;
NSDate *exactDate = [NSDate dateWithTimeIntervalSince1970:unixTimeStamp];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yyy hh:mm a";
NSString *finalate = [dateFormatter stringFromDate:exactDate];
In Swift
let timeStamp = 1513330403393
let unixTimeStamp: Double = Double(timeStamp) / 1000.0
let exactDate = NSDate.init(timeIntervalSince1970: unixTimeStamp)
let dateFormatt = DateFormatter();
dateFormatt.dateFormat = "dd/MM/yyy hh:mm a"
print(dateFormatt.string(from: exactDate as Date))
See the various example on date formatting and getting date from timestamp from apple
Use Formatter Styles to Present Dates and Times With the User’s Preferences
Convert TimeStamp to NSDate in Objective-C
Here is a code piece which lets you convert the unix type timestamp to Formatted Date
NSString * timeStampString =#"1304245000";
NSTimeInterval _interval=[timeStampString doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
[_formatter setDateFormat:#"dd.MM.yyyy"];
NSString *_date=[_formatter stringFromDate:date];
This will print _date something like 05.03.2011