I am using the following code to retrieve date and year from NSString. I am printing them in NSLog statements.
NSDate *dateC= [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm"];
dateC = [dateFormat dateFromString:#"04/15/2009 00:00"]; // Only this date gives null date.
[dateFormat setDateFormat:#"MMM dd, yyyy"];
NSLog(#"date = %#",[dateFormat stringFromDate:dateC]);
NSCalendar* calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:dateC];
NSLog(#"year =%d",[components year]);
[dateFormat release];
This code works fine for every date except one that is mentioned in the code.
The output should be:
date = Apr 15, 2009
year =2009
But the output is:
date = (null)
year =2001
Please help me why am I getting this weird behaviour?
NOTE: I am using Xcode 4.6.2 and iOS 6.1
Please try this exact code. Also note the section under Fixed Formats in the Date Formatters Guide, linked to in the NSDateFormatter class description.
// NSDateFormatter Class Description says:
// Note that although setting a format string (setDateFormat:) in principle specifies an
// exact format, in practice it may nevertheless also be overridden by a user’s
// preferences—see Data Formatting Guide for more details.
NSLocale *locale;
NSLocale *curLocale = [NSLocale currentLocale];
NSLocale *usaLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
if([curLocale.localeIdentifier isEqualToString:usaLocale.localeIdentifier]) {
locale = [[NSLocale alloc] initWithLocaleIdentifier:usaLocale.localeIdentifier];
NSLog(#"Created a new one");
} else {
NSLog(#"USed the old one");
locale = usaLocale;
}
assert(locale);
NSDate *dateC= [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:locale];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
assert(calendar);
[dateFormat setCalendar:calendar];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm"];
dateC = [dateFormat dateFromString:#"04/15/2009 00:00"]; // O
NSLog(#"DATE: %#", dateC);
If it is displaying a different hour you may need to set the default timezone (this happens only after ios7)
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Related
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];
I was using NSDateformatter for parsing a date. It was working fine til I updated to iOS 7.0.4. But since the update I am getting a nil value,
This is the date I am trying to parse
11/20/2013 3:30:05 PM
Below is the code for the same
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter1 dateFromString:[items objectAtIndex:2]];
But I am getting nil as the date.
How can I make this work in IOS 7.0.4?
Your date is localized, but you are using the device its locale.
But the AM/PM may be different in other locales.
-(void)dateTest {
NSString *inputDate = #"11/20/2013 3:30:05 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [dateFormatter1 dateFromString:inputDate];
NSLog(#"date: %#", date); //OUTPUT date: 2013-11-20 15:30:05 +0000
}
Can you post what string is stored in [items objectAtIndex:2]?
I try it and it works fine for me:
-(void)dateTest
{
NSString *inputDate = #"11/20/2013 3:30:05 PM";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = #"MM/dd/yyyy hh:mm:ss a";
[dateFormatter1 setLocale:[NSLocale currentLocale]];
NSDate *date = [dateFormatter1 dateFromString:inputDate];
NSLog(#"date: %#", date); //OUTPUT date: 2013-11-20 15:30:05 +0000
}
If it is displaying a different hour you may need to set the default timezone (this happens only after ios7)
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
it seems that the default timezone is the one the device has, so if you no specify the default timezone you might get strange results. Prior to ios7 it was always GMT.
This is really odd, the code below takes self.danceTimeIn (its text state) and converts it to an actual time. The problem is that its coming up 1 hour LESS than what's entered. Meaning that if I enter 14:03 I'll get 13:03 in the database! The same thing is happening with the date version of this code
++++++++++++++++++++++
TIME
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
DATE
NSString *danceDateValue = self.danceDate.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yy"];
NSDate *date = [dateFormat dateFromString: danceDateValue];
++++++++++++++++++++++
Anyone ???
Try using this. NSDateFormater change date according to locale of your device settings, if you set locale properlyl, you will get proper date. Try if this works :)
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatIn setLocale:usLocale];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
Time zone may be causing this problem.Try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
May be this will help.
It depends on the timezone:-
first Check your local time zone
NSTimeZone *tz=[NSTimeZone timeZoneWithName:#"GMT"];
[dateformat setTimeZone:tz];
and then set your date accordingly.
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 build label with the date for today (hebrew date)
its work but is number and not character
for exapmle:28/4 to 28 Tavat
code:
NSLocale *hebrew = [[NSLocale alloc] initWithLocaleIdentifier:#"he_IL"]; // Hebrew, Israel
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = hebrew;
dateFormat.calendar = calendar;
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
[_label setText:dateString];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"hebrew: %#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
You need to change the style to medium or higher:
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
If you use the medium style then the month will be abbreviated, switch to the long style (NSDateFormatterLongStyle) for the full month name.