How to give UIdatepicker non-gregorian calendar w/ gregorian value - nsdateformatter

Using this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm"];
having defaults of '2014' on my datepicker,
Using Thai calendar gives me headache for displaying a year of 2557 (which is of course in Thai locale) to its year picker on the UIdatepicker. yes that is correct - but I want to get it's value in gregorian calendar format so I could save it on the database without any worries. can I do that on my NSDateFormatter? (also including hours, minutes, and days)

I solved my own problem:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:gregorianCalendar];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];

Related

Get current NSDate with a format of: dd/MM/yyyy

I'm trying to get the current date with the following format: dd/MM/yyyy.
The way I would format it, is like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
And I would get the current date like this: [NSDate date]. How can I mix the 2 together, and get the current date with that format?
If you want to get current day like format this: dd/MM/yyyy.
You can use this code:
NSDate *datecenter = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d, MMMM, YYYY"];
NSString *dateString = [dateFormat stringFromDate:datecenter];
lbldaymonthyear.text = dateString;
This is probably the most succinct way, you can use different NSDateFormatterStyles to change the way the date is displayed later
NSString *date = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle
Specifies a short style, typically numeric only, such as “11/23/37” or “3:30 PM”.
Equal to kCFDateFormatterShortStyle.
More styles here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/#//apple_ref/c/tdef/NSDateFormatterStyle
You can use this code:
NSDate *datecenter = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd, MMMM, YYYY"];
NSString *dateString = [dateFormat stringFromDate:datecenter];
yourlabel.text = dateString;

NSDateFormatter not working to covert NSString to NSDate

I am using NSDateFormatter to get a date from an NSString I am confused as to why the following is not working:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: timeDateUpdated];
myDate remains nil and yet, as the image shows the timeDateUpdated appears to be of the correct format.
Wrong format, use HH instead of hh.
The h directive is for hour in the am/pm format, will not work with 17.
See this comprehensive date formatting guide

NSDateFormatter returning null on some devices

I have problem only with some devices NSDateFormatter returning null the date format from the server is "13:05, 10 November 2013".
NSDate *Now = [NSDate serverDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm, d MMM yyyy"];
NSDate *LastLogin = [dateFormat dateFromString:DateString];
On simulator and few devices works
LastLogin 2013-10-05 00:36:00 +0000 | Now 2013-11-25 14:50:51 +0000
but on some devices
LastLogin (null) | Now 2013-11-25 15:00:22 +0000
The problem seems to be with locale, just set the locale for the formatter:
NSDate *Now = [NSDate serverDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setDateFormat:#"HH:mm, d MMM yyyy"];
NSDate *LastLogin = [dateFormat dateFromString:DateString];
If you're not setting a particular locale, numbers and dates may be parsed differently even with fixed date formats.
The docs have this to say;
If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences.
Extending your code slightly;
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDate *Now = [NSDate serverDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:enUSPOSIXLocale];
[dateFormat setDateFormat:#"HH:mm, d MMM yyyy"];
NSDate *LastLogin = [dateFormat dateFromString:DateString];

Get the time and date of selected time zone?

I tried below snippet I'm getting current date and time of system instead of the selected time zone code.
NSDate *now = [NSDate date];
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]];
NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setDateFormat:#"dd-MM-yyyy HH:mm:ss "];
NSString *strSelectedDate= [formatter stringFromDate:selectedDate];
setZone.text = strSelectedDate;`
Any ideas?
[NSDate date]returns a date object representing the current date and time, no matter where you are. NSDates are not subject to places or time zones. There is just one NSDate that represents now or any other moment for that matter, not different date objects for every time timezone. Therefore, you should not attempt to convert a date between time zones.
NSDate objects represent an absolute instant in time. Consider the following example of how two date representations in different time zones (9/9/11 3:54 PM in Paris and 9/9/11 11:54 PM in Sydney) are actually the same date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSDate *aDate = [formatter dateFromString:#"9/9/11 3:54 PM"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSDate *anotherDate = [formatter dateFromString:#"9/9/11 11:54 PM"];
NSLog(#"%#",anotherDate);
if ([aDate isEqualToDate:anotherDate]) {
NSLog(#"How about that?");
}
When it comes to output a date, bear in mind that NSDate's description method returns time in GMT and you need to use a NSDateFormatter to create a date string representing the local time in Paris, Sydney, etc. from a date:
NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Australia/Sydney"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
NSLog(#"%#",[formatter stringFromDate:now]); //--> 9/9/11 3:54 PM
[dateFormatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss z"];
Remove the trailing 'z' character from the format string if you don't want to display the time zone.
EDIT
On the other hand, if you just want to display the timezone name, just make the 'z' uppercase.
EDIT
Lowercase 'z' works fine for all the other timezones, but unfortunately GMT is a special case. So the easiest thing to do is to just omit the 'z' and append " GMT" to the formatted date.

Convert RSS date format to german date in Xcode

I have a little App which shows data from an RSS feed. Because it's date format is very long and now well for being printed out I want to convert it to the German's date format. This code I have now, but it always return "nil":
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormat setDateFormat:#"dd MMM, yyyy"];
NSString *contentDate = [dateFormat stringFromDate:date];
What's wrong with it? Thanks for answers!
RSS dates are a bit tricky.. you can grab the NSDate+InternetDateTime.h/.m from here:
https://github.com/mwaterfall/MWFeedParser
that should take care of the hard work.