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];
Related
I need to display today date format like 16 Mar,Monday,Here my code is
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM, EEEE"];
dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
I am getting output is 16MAR,MONDAY ,But my output should be 16 Mar, Monday.Thanks
Try this
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM,EEEE"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"Date String %#",dateString);
I am getting same output that you want
Hope you will got success..
How to convert a date of any language(Arabic) to a specific language(English).
While changing the region format to arabic, the dates are getting changed. When I am picking the date value from some controls(UIButtons or UILabels) for saving in database, its picking the date in arabic language and saving in the same format. Here I want to convert the date from arabic to english before saving it in database.
I tried this but its returning nil
NSDate *currentDate = //My date coming like “٢٠١٤-٠٥-١٥ ١٢:١٠:٢٣ ”
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale: usLocale];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:s a"];
NSString *formattedDate = [formatter stringFromDate: currentDate];
Can anyone please help me ?
I solved in this way
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:s"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
NSString *dateString = [dateFormatter stringFromDate:myDate];
You shouldn't store dates in any language. You should store NSDate, which has no language or locale or time zone, and display it as appropriate. If your database cannot store NSDate, you can store [myDate timeIntervalSince1970] and convert it to an NSDate using the class method dateWithTimeIntervalSince1970:
You should use UIDatePicker as the input instead of UIButtons or UILabels. Save the timeIntervalSince1970 into your database, so that it can be presented by any language.
NSDate *currentDate = //My date coming like “٢٠١٤-٠٥-١٥ ١٢:١٠:٢٣ ”
NSTimeInterval timeToBeSavedInDatabase = [currentDate timeIntervalSince1970];
//in the future, you can show this date in any language
NSString *dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970]];
You may use this to get the date with preferred local
NSDate * dateNow = [NSDate new];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale: usLocale];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString * dateString = [dateFormat stringFromDate:dateNow];
In my projet I am getting date value in the below format,
NSString* str = #"04-10-2011 20:00:00";
Then I am trying to convert it to date
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
NSDate *date = [df dateFromString:str];
It is getting nil. Am I giving wrong date format? Can any one let me know?
Your code seems pretty, try to use timezone property of dateformatter to get the exact date
NSString* str = #"04-10-2011 20:00:00";
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:locale];
[df setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
NSDate *date = [df dateFromString:str];
NSLog(#"Date is %#", date);
[df setDateFormat:#"mm-dd-yyyy HH:mm:ss"];
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 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]];