iOS:Objective-C:Output issue due to ipad time display 12Hour / 24Hour - ios

In my application i am using one function to change date format to replace / with - and date format also.
When Device is set to 12 Hour, everything works fine. But when i set it to 24Hour it returns wrong value.
NSDate *newdate = [self convertDateSlashToDash:[obj valueForKey:#"TaskStartDateTime"]]);
//input date is : 6/6/2017 6:38:00 PM
-(NSDate *)convertDateSlashToDash:(NSString *)dateStr{
if ([dateStr isKindOfClass:[NSDate class]]) {
return (NSDate*)dateStr;
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the AM and PM symbols
[dateFormatter setAMSymbol:#"AM"];
[dateFormatter setPMSymbol:#"PM"];
//Specify only 1 M for month, 1 d for day and 1 h for hour
[dateFormatter setDateFormat:#"M/d/yyyy h:mm:ss a"];
// in ms 1469077819000 without ms 1469077819 for 7/21/2016 5:10:19 AM
NSTimeInterval ti = [[dateFormatter dateFromString:dateStr] timeIntervalSince1970];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:ti];
NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
// [formatter setLocale:[NSLocale currentLocale]];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss aa"];
NSString *dateString = [formatter stringFromDate:date];
NSDate *parsedDate = [formatter dateFromString:dateString];
return dateString;
}
//output of this is (24hrs): 1970-01-01 05:30:00AM
//output of this is (12hrs): 2017-06-06 06:38:00 PM
Why it is not working?
Please suggest.
Thank you.

Please add this line to your code to resolve that problem
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
Reade more: https://developer.apple.com/library/content/qa/qa1480/_index.html

The 12-hour and 24-hour are specified differently with a date formatter. Make sure that you are using the right format. I see you are using h for hour along with a at the end which configures the formatter to a 12-hour format. For a 24-hour format, you need H or HH depending on your needs. You can refer to this link to get a better understanding of different formats

Related

Getting wrong time while converting from NSString to NSDate When Device Time format is 24hr

In Following Code I am trying to convert NSString to NSDate and once again converting to another Date format and sending it to server.
Following code worked perfectly fine when my Device Time Format is 12 hr. with dateformat to [dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"]; it gives me correct Date and Time.
But As soon as I changed device time Format to 24 hr. and change the datefomatter to [dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"]; it gives wrong Date and Time (Specifically time.) I had shown log values for code.
I had checked lot of stackoverflow questions regarding NSDateFormatter and Conversion of NSString to NSDate. But I am not able to find out where I'm making mistake and why it gives me wrong time when my Device time format is 24hr. Please help me to resolve this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
Following log showing Date and Time When My Device Time format is 12hr. And it is Correct one.
From DateTime 2017-07-04T13:46:00
Time When My Device Time format is 24hr. And it is wrong (specifically time).
From DateTime 2017-07-04T12:46:00
Please try setting locale & timezone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: #"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);

Converting calendar date to the correct date

I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);

Current Time in Date format shows wrong - ios

I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;
// getting today's date string
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
[dateFormatterToday setDateFormat:#"yyyy-MM-dd HH:mm a"];
NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];
// converting today's date string to NSDATE
NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
[dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm a"];
dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];
Here currentDateTimeString, which is in string format showing my current time as :
2016-01-11 17:52 PM (which is correct one)
but dateTodayFinal, which is in Date format shows:
2016-01-11 07:22:00 +0000
I have tested with different timezones, but the issue persist, please help some one. Thank you.
The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.
Add:
NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(#"dateTodayFinalTimeString: %#", dateTodayFinalTimeString);
Output:
dateTodayFinalTimeString: 2016-01-11 07:57 AM
NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.
If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];
// converting today's date string to NSDATE
//
// NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
// [dateFormatterTodayFinal setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];
Also have a look at the docs.

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

Resources