Dateformatter gives wrong time on conversation [duplicate] - ios

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
I am trying to convert my date string to NSDate but its return correct date and wrong time.
This is my code :
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
It returns this :
lastUpdatedate : 2013-12-20 06:40:40 +0000

- [NSDate description] (which is called when passing it to NSLog) always prints the date object in GMT timezone, not your local timezone. If you want an accurate string representation of the date, use a date formatter to create a correct string according to your timezone.

As #Leo Natan said, - [NSDate description] always gives date in GMT timezone. If you want to convert into local timezone then use following code.
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",[self getLocalTime:lastUpdatedate]);
-(NSDate *) getLocalTime:(NSDate *)date {
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
return [NSDate dateWithTimeInterval: seconds sinceDate: date];
}
OUTPUT:
lastUpdatedate : 2013-12-20 12:10:40 +0000

NSString *dateStr = #"2013-12-20 12:10:40";
NSDateFormatter *dateFormatterTest = [[NSDateFormatter alloc] init];
[dateFormatterTest setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatterTest setLocale:[NSLocale currentLocale]];
NSDate *d = [dateFormatterTest dateFromString:dateStr];
Set NSLocale in your code, and you get perfect result.

You can try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSTimeInterval sourceGMTOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:lastUpdatedate];
lastUpdatedate = [lastUpdatedate dateByAddingTimeInterval:sourceGMTOffset];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);

Related

How to convert UTC date string to local time

I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.

Incorrect date conversion from string [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
I am trying to convert my date string to NSDate but its return correct date and wrong time.
This is my code :
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
It returns this :
lastUpdatedate : 2013-12-20 06:40:40 +0000
- [NSDate description] (which is called when passing it to NSLog) always prints the date object in GMT timezone, not your local timezone. If you want an accurate string representation of the date, use a date formatter to create a correct string according to your timezone.
As #Leo Natan said, - [NSDate description] always gives date in GMT timezone. If you want to convert into local timezone then use following code.
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",[self getLocalTime:lastUpdatedate]);
-(NSDate *) getLocalTime:(NSDate *)date {
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
return [NSDate dateWithTimeInterval: seconds sinceDate: date];
}
OUTPUT:
lastUpdatedate : 2013-12-20 12:10:40 +0000
NSString *dateStr = #"2013-12-20 12:10:40";
NSDateFormatter *dateFormatterTest = [[NSDateFormatter alloc] init];
[dateFormatterTest setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatterTest setLocale:[NSLocale currentLocale]];
NSDate *d = [dateFormatterTest dateFromString:dateStr];
Set NSLocale in your code, and you get perfect result.
You can try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSTimeInterval sourceGMTOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:lastUpdatedate];
lastUpdatedate = [lastUpdatedate dateByAddingTimeInterval:sourceGMTOffset];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);

Which dateFormat should I choose while converting NSString to NSDate?

I have a NSString like #"2014-11-27T10:54:08.185Z"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate * date = [dateFormatter dateFromString:self.creationTime];
I tried a lot of different formatter strings, but the date is always nil.
Or is there another problem here?
The hour in your date string format seems to be in the 24h format.
So you need to use HH instead of hh in your dateFormat.
More info
Try
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.creationTime];
self.dateLabel.text = [formatter stringFromDate:date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
Please try this...:)

Time between date from string and current time in GMT

I want to find the time (in minutes) between a date with the "yyyy-MM-dd'T'HH:mm:ss" format (Sample:"2014-02-03T11:28:00") and current time (In GMT, as the string is in GMT. I tried
NSDateFormatter *formatter;
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *now = [NSDate date];
NSDate *trackingDataDate = [formatter dateFromString:timeStamp];
NSTimeInterval distance = [now timeIntervalSinceDate:trackingDataDate];
double timeInMinutes = distance / 60;
But this returns NaN. What is wrong with my code, how can I get the time between the events?
Simple mistake. You have miss allocation for NSDateFormatter. Add below line instead of NSDateFormatter *formatter;, It's working fine.
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
Update: According to your comment, see my below code for convert to GMT Date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
Try this:
NSString* string=#"26-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:string];
NSLog(#"Date = %#",date);

Getting null when trying to extract year value from a date [duplicate]

This question already has answers here:
NSDate from NSString gives null result
(3 answers)
Closed 9 years ago.
My code is the following:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);//null
How to fix that? thanx.
It is because you are using unix timestamp and you can convert unix timestamp into year or any date format as :
//Posted Date Format
NSString *dateStr = #"1339007317";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy"]; //Here you can set any date format Ex:#"dd MMM, yyyy hh:mm a" or#"dd/MM/yyyyy" according to your requirement
[dateFormatter1 setLocale:locale];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatter1 stringFromDate: date];
Hope it helps you.
You need two date formats. The first should match the format of the string you wish to convert to an NSDate. The second format needs to represent the format you want. Use the 2nd to convert the NSDate to the new string.
Also, why do you create a date object then immediately replace it with a new one?
Change this:
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
to:
NSDate *date = [dateFormatter dateFromString:dateString];
Your code needs to be like this:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???"]; // format that matches the dateString
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"yyyy"]; // the desired new format
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);

Resources