I am having problem displaying proper date in my app. When user sets 24 hour format 'ON' in his date time preferences..my app shows correct time but when 24 hour format is set to 'OFF' it shows nothing.
Here is the code I am using:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *temp = [formatter dateFromString:strDate];
[formatter setDateFormat:format];
[formatter setLocale:[NSLocale currentLocale]];
NSString *returnStr = [formatter stringFromDate: temp];
[formatter release];
NSLog(#"Return: %#", returnStr);
return returnStr;
Try this
[dateFormat setDateFormat:#"hh:mm:ss"];
HH : 24 hour format
hh : 12 hour format
So in your case
//after conversion to date
if ON
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
if OFF
[formatter setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
Very simple try this:
For 12 Hrs : (hh:mm:ss)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
For 24 Hrs : (HH:mm:ss)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
use this:
Objective-C :
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
Swift 4.1
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
after that your date won't modified.
Set the locale of the dateFormatter as following. It should sove your problem.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatter setLocale:locale];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
Related
i came across a problem in my iPhone application.
in my app i converte time string to date like that
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
NSDate *date = [dateFormat dateFromString:time];
and for some reason always date is nil after [dateFormat dateFromString:time].
can anyone tell me what i'm doing wrong?
thanks
I'm assuming your device language is other then English. In that case you need to set the date formatter Locale. Try this code.
NSString *time = #"01:30 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
try this...
NSString *time = #"01:30 pm";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeStyle:NSDateFormatterShortStyle];
[dateFormat setDateFormat:#"hh:mm a"];
dateFormat.timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
NSDate *date = [dateFormat dateFromString:time];
NSLog(#"date %#",[dateFormat stringFromDate:date]);
NSString *dats1 = #"01:30:00 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter1 dateFromString:dats1];
NSLog(#"date : %#", date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSLog(#"Current Date: %#", [formatter stringFromDate:date]);
I have datetime format like this
2015-10-06 12:39:12
when i try to convert into nsdate it return me null. i am using below code for it.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [formatter dateFromString:user_post.modification_date];
Please tell me how can i convert it into correct NSDate?
dateString is your date in string format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
Try to set like this
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
Change this line only.
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Z is the timeZone
I suggest you to set the timezone, can be the cause of the problem
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
Trying to convert a datetime from NSString to NSDate using NSDateFormatter. I know it should be a simple task, but it's not - the converted NSDate is whole 20 days off.
I cannot find the cause for this. Already tried every solution I could find (different locales, time zones, formattings ...) but no luck so far.
Here's a code sample:
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"dd.MM.YYYY HH:mm:ss"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CET"]];
NSDate *dte = [dateFormat dateFromString:strDate];
output of dte is "04.01.2014 08:17:10". As you can see, the time is correct, but the date is way off.
There is an error in your time format, YYYY should be yyyy.
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CET"]];
NSDate *dte = [dateFormat dateFromString:strDate];
Will give me as output : 2014-01-24 07:17:10 +0000
Use the following code
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:strDate];
Interesting issue thats caught me out.
I receive string times from a server to a device. which I then convert in to a NSDate.
When the device was set to displaying 24hour times, life was good.
Now I am testing it on a device set to 12hour times. everything has stopped working. Dates are coming back as null
I first had
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
Worked perfectly for devices showing 24hour dates but not 12hour.
I then tried
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
This works fine up until 12 noon, then all dates are returned as null
Update
I have also tried adding "a" but this still results in returning null
if (startDate == nil)
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
}
Update 2
Adding local, adding :ss adding a all still do not work
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:#"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
It's close... I think you need:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"HH:mm"];
startDate = [dateFormatter dateFromString:(NSString *)self.startTime];
You can try this
12 to 24 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"hh:mm a"];
NSDate* newDate = [df dateFromString:LocationTrackingStartTime];
[df setDateFormat:#"HH:mm"];
newDate = [df stringFromDate:newDate];
24 to 12 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"yyyy-mm-dd hh:mm:ss"];
NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
[df setDateFormat:#"hh:mm a"];
newDate = [df stringFromDate:[NSDate date]];
You have to add am/pm:
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:#"hh:mm: a"];
Change 24 hours format to 12 hour format,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set 24 hours date format
dateFormatter.dateFormat = #"HH:mm";
//Here #"your24hours date string" is your string date with 24 hours format
NSDate *date = [dateFormatter dateFromString:#"your24hours date string"];
//Set 12 hours date format
dateFormatter.dateFormat = #"hh:mm a";
NSString *strDate = [dateFormatter stringFromDate:date];
NSLog (#"%#", strDate);
//Output is in 12 hours date format.
You wanted to convert an NSDate forced to NSString to an NSDate again.
First you have to convert it to and NSString, not forcing it!
I also support 12 and 24 hour formats and i have no problem with it.
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:strDate4Convert];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *newTimeStr = [df stringFromDate:newDate];
You just need to set the locale to take the am pm format. And everything else as usual.
NSDateFormatter * df = [[NSDateFormatter alloc]init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:locale];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", [df stringFromDate:pickUpDate]);
I am using following code to get a date from string but its not showing correct date
NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];
[saveddf setDateFormat:#"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];
Saturday_June 22_2013 11:44 AM -- INPUT (datestring).
2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).
Please tell whats wrong in my code.
Try this code, it should work:
NSString *yourStringDate = #"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"EEE_MMM dd_yyyy hh:mm a"];
NSDate *date = [dateFormatter dateFromString: yourStringDate];
//Set the required date format
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
Try to use this code
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(#"%#",dateString);