There is differnce in date while I convert string to date - ios

I am using following code to get date from string. All seems to be good in code but while I print the output date, there is difference of 12:30 hours in date.
What may be the issue? Am I missing something ?
NSString *strDate = #"8/22/2017 7:00:00 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [df dateFromString:strDate];
NSLog(#"%#", [date description]);
Output:
2017-08-21 18:30:00 +0000

The hour specifier is wrong, 12 hour format is hh
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
Note:
Be aware that NSLog prints the date always in UTC although the date formatter considers the local time zone.

try this:
you just set the time to GMT
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

Related

NSDateFormatter: error in converting date

I have got the following time stamp in milliseconds:
NSDate * date = [NSDate dateWithTimeIntervalSince1970:1470524933.923123];
NSDate * date2 = [NSDate dateWithTimeIntervalSince1970:1470666561.000];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm:ss"];
NSString * dateInString = [dateFormatter stringFromDate:date];
When I run it I get the following:
2016-08-07 at 00:08:53, 2016-08-08 at 15:29:21
However the first date should be as following according to the epoch time converter website I am using:
Your time zone: 8/7/2016, 12:08:53 AM GMT+1:00 DST
It says 00 rather than 12. Why is that?
Try to set the TimeZone of your NSDateFormatter to GMT
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
As larme and vadian suggested if you want 12 for hour use this format
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' hh:mm:ss a"];

Convert NSDate in12 Hour Format To 24 Hour Format [duplicate]

This question already has answers here:
Convert NSDate to NSString
(19 answers)
Closed 8 years ago.
I have an NSDate Object which is in 12 hour format like 2014-09-16 04:40:05 pm +0000.
I want to convert this into 24 hour format and want to get back an NSDate object like 2014-09-16 16:40:05 +0000.
Can some one guide me in doing that.
So i want some method like :
-(NSDate *) get24HourFormat:(NSDate *) date{
return date object;
}
Simple use this:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
dateformate.dateFormat = #"HH:mm a"; // Date formater
NSString *timeString = [dateformate stringFromDate:[NSDate date]]; // Convert date to string
NSLog(#"timeString :%#",timeString);
Logic: Simply convert date format hh:mm to HH:mm
There seems to be a deep misunderstanding here what NSDate is and does.
An NSDate object is a point in time in UTC. It doesn't have a time zone. It doesn't have a format. It has nothing. You can't change its format, it doesn't even make any sense.
What you can do is to use an NSDateFormatter to convert the NSDate to a string. By default, NSDateFormatter uses your local time zone, which means for most people that the result will be different from the result that NSLog would show for an NSDate. In the NSDateFormatter, you can use whatever settings you want.
Usually you would respect how the user set up his date formatting and not change it for anything that is visible to the user. As a user, if I had set up my device to show days in 12 hour format, I'd be very annoyed if your application worked differently.
try this..
-(NSString *)changeformate_string24hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
return [df stringFromDate:wakeTime];
}
-(NSString *)changeformate_string12hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
return [df stringFromDate:wakeTime];
}

NSDateFormatter returning AM when should be PM

I have a NSDate that I want to display in 4:00 PM form.
NSDate *time = [object objectForKey:#"time"];
NSLog(#"time: %#", time);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale currentLocale];
[dateFormatter setDateFormat:#"h:mm a"];
timeLabel.text = [dateFormatter stringFromDate:time];
The log returns:
2014-03-08 13:00:00 +0000
But timeLabel.text reads 1:00 AM, when it should be pm. Any idea what's going on?
You forgot to add the timezone. The rest is fine. You have to give the formatter a timezone or it will use your local one
also be aware that logging with NSLog ALWAYS prints UTC dates
use [formatter setTimezone:timeZoneXY]
you can use following code:
NSString *str=#"17-02-2014 05:00 PM";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *datetoday=[dateFormatter dateFromString:str];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
NSString *strdate=[dateFormatter stringFromDate:datetoday];
NSLog(#"%#",strdate);
NSDate *time = [object objectForKey:#"time"];
NSLog(#"time: %#", time);
Your time variable time format is have 24 hour format. that way it display NSLog 13.00.
After that you have use to converter 12 hour format thats way it display am/pm style
[dateFormatter setDateFormat:#"h:mm a"];
Note:
HH - captial letter H is denote 24 hour format
hh - small letter h is denote 12 hour format

Remove time zone offset from date in iOS

I need to get the current NSDate.date and remove the time zone and parse it as a GMT date
NSDate.date
returns 2012-10-11 11:27:09 -0700
What I need is this: 2012-10-11 11:27:09 +0000
NSDate.date returns a date with the current date and time stored as GMT.
If you want to format the date as a string and show the GMT time, you should use a NSDateFormatter and set the locale to GMT:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"GMT"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *dateAsString = [formatter stringFromDate:[NSDate date]];

Incorrect output from NSDateFormatter

I'm getting some strange output from a NSDateFormatter. I'm converting a date from GMT to the system time zone, which is EDT. This should be -4 hours from GMT.
-(NSDate*)parseDate:(NSString*)inStrDate {
NSLog(#"Date To Parse %#", inStrDate);
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setLocale:[NSLocale systemLocale]];
[dtFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dtFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss +0000"];
NSDate* dateOutput = [dtFormatter dateFromString:inStrDate];
NSLog(#"Parsed Date %# %#", dateOutput, [NSTimeZone systemTimeZone]);
return dateOutput;
}
Log output:
2012-09-15 22:32:53.358 Date To Parse 2012-09-16 02:32:53 +0000
2012-09-15 22:32:53.360 Parsed Date 2012-09-16 06:32:53 +0000 America/New_York (EDT) offset -14400 (Daylight)
But it's returning +4 hours instead of -4. So where it should output 22.30 EDT (02.30 GMT) it's actually returning 06.30 EDT. Which is 8 hours in the future.
Can anyone help me understand if I'm going wrong somewhere here? I'm scratching my head but I can't figure out why this wont seem to work.
Thanks
You just got the dateFormatter the wrong way around.
When you do
[dtFormatter setTimeZone:[NSTimeZone systemTimeZone]];
you're telling it that the input Date is in terms of your time zone. Therefore, it converts it to GMT and outputs the time +4 hours. To do it the other way around you can use this:
NSDateFormatter* df_local = [[NSDateFormatter alloc] init];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"America/New_York"]];
[df_local setDateFormat:#"yyyy.MM.dd' 'HH:mm:ss zzz"];
NSDate* dateOutput = [dtFormatter dateFromString:dateString];
NSLog(#"Parsed Date in GMT %#", dateOutput);
NSString *yourLocalDate = [df_local stringFromDate:dateOutput];
NSLog(#"in EDT %#",yourLocalDate);
This logs
2012-09-16 05:23:53.297 TestingApplication[10109:c07] Date To Parse 2012-09-16 02:32:53 +0000
2012-09-16 05:23:53.302 TestingApplication[10109:c07] Parsed Date in GMT 2012-09-16 02:32:53 +0000
2012-09-16 05:23:53.304 TestingApplication[10109:c07] in EDT 2012.09.15 22:32:53 EDT
if you want a date as a final output you can skip the secondary NSDateFormatter and just 'add' the difference between the UTC date and the local time.
formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:dateString]; // UTC date
NSDate *date2 = [date dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMTForDate:date]]; // local date!

Resources