I have these following snippet.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat : #"yyyy/MM/dd"];
NSString *timeStr = #"2015/05/16";
NSDate *timeDate = [formatter dateFromString:timeStr];
But when I print timeDate in console,the output is werid.The year becomes 4003.
4003-05-16 16:00:00 +0000
I test it in iPad 8.3(12F69)(not simulator).System time zone is Beijing.
Any help is appreciated.
The issue is the locale of the formatter. If you want to use gregorian calendar regardless of the device settings, you generally should set locale to en_US_POSIX.
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
See Apple Technical Q&A #1480. This is geared towards RFC 3339/ISO 8601 dates, but it really applies anywhere you're trying to use standard calendar for converting date string for internal purposes (i.e. as opposed to those dates that are presented to the end user in the UI, which generally should use the device's locale).
Try this... Just need to add last line while printing output.
NSString *timeStr = #"2015/05/16";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd";
NSDate *Date = [formatter dateFromString:timeStr];
NSLog(#"Date =%#", [formatter stringFromDate:Date]);
Related
I am trying to set the date to PayPal pre-approval key in the following way:
#"2015-04-27T10:45:52Z", #"startingDate",
This date works, however I don't know how to reproduce it in code terms. I tried doing:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Date formater
NSString *date = [dateformate stringFromDate:[NSDate date]];
but this doesn't work. What is the Z at the end of the date?
First, your date format is not correct. Second, for consistent results, you should always hard-code the en_US_POSIX locale (the date formatter defaults to the user's locale):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
Alternatively, I've had positive experience with iso-8601-date-formatter. ISO 8601 is a surprisingly complex standard with lots of edge cases, and this library seems to be able to cope with most of them.
The Z stands for Zulu (i.e. UTC/GMT). If you want to generate a date string in that format (GMT with Z qualifier), please refer to Apple's Technical Q&A #1480, which reminds us to specify both locale and timeZone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Or in macOS 10.12 and iOS 10, you can do:
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
And then you can do:
NSString *dateString = [formatter stringFromDate:[NSDate date]];
This question already has answers here:
NSString to NSDate
(5 answers)
Closed 9 years ago.
I'm trying to get the date "30/06/2013" from the button:
I tried this code but I got in the log: 2013-01-04 22:00:00 +0000:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/YYYY"];
NSDate *date = [formatter dateFromString:dateBtn.titleLabel.text];
NSLog(#"date %#",date);
and in the app i got this date "05/01/2013":
I also tried this code but it didn't work:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"he_IL"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:#"dd/MM/YYYY"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:dateBtn.titleLabel.text];
date1 = [date1 dateByAddingTimeInterval:interval];
It is important to note I use locale Hebrew.
Have a look at Apple's official documentation on date formatters. Please note how the format differs slightly across different platforms and versions.
Also note the difference between yyyy and YYYY.
set date format to "dd/MM/yyyy"
NSString *originalDate = #"30/06/2013";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [formatter dateFromString:originalDate];
NSLog(#"date %#",date);
The explanation from #fzwo apple link
It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
I've recently been working on a French version of our application use NSLocalizedStrings, so far everything is working great.
But my problem now is with dates. I show dates often in my application, in different formats, depending on the situation.
Ex:
-Fri Feb 22, 2013
-Monday February 18, 2013
-Feb 18
-Dec 5, 2012
The thing is, dates in French are not only different in terms of the name of the month, but also the order in which month, day and year appear.
Ex:
-Dec 5, 2012 would be 5 Dec 2012
-Monday February 18, 2013 would be Lundi le 18 Fevrier 2013.
I have the individual month/day names in my Localizable.string files, but how do I manage the order of how it's displayed.
Should I have an if statement that checks the current device language like so?:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
if([currentLanguage isEqualToString:#"fr"])
{
//Handle French logic
}
This is probably not the best way to go about it.
Any ideas?
You should use an NSDateFormatter and feed it your desired NSLocale like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"];
dateFormatter.dateFormat = #"EEEE d MMMM yyyy HH:mm";
'EEEE' is the full name of the day of the week, which, in my case, will be displayed in dutch.
Use NSDateFormatter. For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(#"formattedDateString: %#", formattedDateString);
NSDateFormatterMediumStyle will automatically format the date according to the user's preference (English, French, etc.).
If you need custom styling and the app runs in iOS 4.0+, you can use a custom template in your date formatter:
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMMyyy" options:0
locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"todayString: %#", todayString);
Set proper locale to your NSDateFormatter when you convert date to string, then date formatter will handle all specifics of formats depending on user settings for you:
NSDateFormatter *formatter = ... // Create and setup formatter
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
// Now you can convert date to string
That's probaly much easier:
There is something which is called
NSDateFormatterShortStyle,
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
Set Date and Time components individually:
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
Ios will format that correctly if the language of the user.
Further look at Data Formatting Guide
I would try the class function localizedStringFromDate:dateStyle:timeStyle: in NSDateFormatter before attempting to use the NSDateFormatter and a template (from Unicode Technical Standard #35):
Example:
[NSDateFormatter localizedStringFromDate:dateTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
You can select both different date and time outputs of various lengths detailed in the docs.
Apple documentation: NSDateFormatter localizedStringFromDate:dateStyle:timeStyle:
I have set the locale and timezone but when I format the date, I always get invalid date. The month is always December and year is one less that the specified year. In my case I dont need the day component.
I checked other similar post but it didn't solved the problem.
Any help is appreciated. Thank you.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setDateFormat:#"MMM YYYY"];
NSDate *formattedDate = [dateFormatter dateFromString:#"Sep 2013"];
NSLog(#"Date: %#", [dateFormatter stringFromDate:formattedDate]);
[dateFormatter release]; dateFormatter = nil;
OUTPUT: Date: Dec 2012
"YYYY" format is used for "Week of Year" based calendars. Use "yyyy" format specifier instead:
[dateFormatter setDateFormat:#"MMM yyyy"];
I just wanted to add something to the great answer by Vladimir. If you do this before setting your locale, it seems that the date formatting goes crazy. What I had to do was to set the locale before setting the new format, and then use the setDateFormat to change the format based on the locale used.So something like this would do:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];//I wanted to set the locale to wherever user is using my app in
[formatter setDateFormat:#"MMM YYYY"];
NSDate *now = [NSDate Date];
NSString *fancyLookingDate = [formatter stringFromDate:now];
I am getting start and end dates for my calendar event(while parsing a .ics file) in "20110912T220000" format. How can I convert this to a NSDate to add to add as event(EKEvent)'s startDate property.
If anyone knows please help me soon.
You should use NSDateFormatter for this.
See Data Formatting Guide (the Date & Time Programming Guide may also be interesting)
This is also detailed in this Technical Note in Apple's Q&As. Note that for such situations, you should use the special "en_US_POSIX" locale as explained in this technical note.
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[df setDateFormat:#"yyyyMMdd'T'HHmmss"];
NSDate* parsedDate = [df dateFromString:...];
NSString *dateString = #"20110912T220000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
formatter.locale = locale;
formatter.dateFormat = #"yyyyMMdd'T'HHmmss";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog() output: date: 2011-09-13 02:00:00 +0000
Note, NSLog outputs the data in you local timezone.
Note the single quotes around the 'T' in the date format.
Here is a link to UTS: date/time format characters