I am trying to convert a date into a different format. I'm receiving my date as an NSString with the following format: EEE MMM dd HH:mm:ss ZZZ yyyy, and am attempting to change it to this format: dd-mm-yy. However, I am not able to get it in desired format.
This is my current code:
NSString *dateStr = [NSString stringWithFormat:#"%#",[dict valueForKey:#"createdOn"]];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"IST"];
dateFormatter.timeZone = gmt;
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSDate *dateFromString = [dateFormatter dateFromString:dateStr];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:dateFromString];
The locale en_US doesn't understand the IST time zone abbreviation. But en_IN does:
NSString *dateStr = #"Tue Mar 24 08:28:48 IST 2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDate *dateFromString = [dateFormatter dateFromString:dateStr];
As John Skeet points out, the issue probably stems from the fact that IST is not unique. IST stands for both Israel Standard Time, and India Standard Time. Thus, when you specify India locale, it makes a reasonable assumption, but for US locale, it is understandably confused.
Unrelated, but make sure to use MM rather than mm in your output formatter:
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:dateFromString];
Related
I know that it sounds incessantly familiar, but most of the suggested solutions on SO have not worked for me for some strange reason.
I have a date string returned from an SQLite query as an NSString in this format:
2019-06-10 13:45:33
However, when any of the suggested date formatter solutions are applied, with or without timezone localisation, I keep getting such a result:
Mon Jun 10 13:45:33 2019
This is one of the routines I've tried, among many others:
NSString * dateString = #"2019-06-10 13:45:33";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
// dateFromString > Mon Jun 10 13:45:33 2019
Could I be doing something wrong or is there some missing step in the conversion?
TIA.
I could guess that you wanted another output format, if it is the case then you could try code like this:
NSString * dateString = #"2019-06-10 13:45:33";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDateFormatter *printDateFormatter = [[NSDateFormatter alloc] init];
printDateFormatter.dateStyle = NSDateIntervalFormatterMediumStyle;
printDateFormatter.timeStyle = NSDateIntervalFormatterMediumStyle;
NSLog(#"%#", [printDateFormatter stringFromDate:dateFromString]);
The result will be:
10 Jun 2019 at 13:45:33
Use a different formatter to format the string from the date. For example:
NSDateFormatter * formatter=[[NSDateFormatter]alloc]init];
formatter.dateStyle = NSDateFormatterMediumStyle;
formatter.timeStyle = NSDateFormatterNoStyle;
NSString * formattedDateString = [formatter stringFromDate:date];
setDateFormat is for inputting date strings and getting NSDates.
dateStyle and timeStyle is for formatting dateStrings from NSDates.
NSString * dateString = [NSString stringWithFormat: #"%#",item.date];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd / M / Y 'ás' HH:mm ZZZZ"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
The data comes in like.... 2014-08-15T08:30:00-04:00
I want this date to be .... 15 - 08 - 2014 12:00
I want the date to be displayed in GMT format, or at least to show the right local time that the event starts, wether you live in US or Japan, and to not display the GMT timezone.
I've tried Z ZZ ZZZ ZZZZ ZZZZZ z zz zzz zzzz
nothing... can anyone help?
try this code
NSString * dateString = #"2014-08-15T08:30:00-04:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"date--%#", [dateFormatter stringFromDate:myDate]);
output--`date--15-08-2014 18:00:00`
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];
I am having problems with conversion of NSString object to NSDate. Here is what I want to do:
I am downloading an RSS Message from the internet. I have the whole message parsed by NSXMLParser. After that, I have parts like , or saved to particular NSStrings. I want to convert element (that includes publication date of RSS Message) to NSDate so that I could perform some operations on it like on a date object (e.g. sorting, showing on a clock etc.). Here is the way my looks like:
"Wed, 25 Sep 2013 12:56:57 GMT"
I tried to convert it to NSDate in this way:
*//theString is NSString containing my date as a text
NSDate *dateNS = [[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss ZZZ"];
dateNS = [dateFormatter dateFromString:theString];*
However, after doing above code, dateNS always appear to be (null).
My question is simple: what is the right way to convert NSString with date formatted like this to NSDate object?
By the way, I have seen the website
http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
It seems that there are many ways to format particular date, but I could not find what I am doing wrong.
Your problem is the your date formatter has not identical fort as your date string:
You should set date formatter the same format like your date string
My Example:
// Convert string to date
NSString *beginString = #"Sat, 30 Dec 2013 14:45:00 EEST";
//beginString = [beginString stringByReplacingOccurrencesOfString:#"EEST" withString:#""];
//beginString = [beginString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"]];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Helsinki"]];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
dateFromString = [dateFormat dateFromString:beginString];
//NSLog(#"Begin string: %#", beginString);
//NSLog(#"not formated: %#", dateFromString);
// Convert Date to string
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ru_RU"]];
[dateFormat setDateFormat:#"dd MMMM yyyy"];
myStrDate = [dateFormat stringFromDate:dateFromString];
[currentTitle setPubDate:myStrDate];
NSDate * dateNS = [[NSDate alloc] init]; is useless, you don't need to allocate any date object.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
NSLog(#"%#", [dateFormatter dateFromString:#"Wed, 25 Sep 2013 12:56:57 GMT"]);
Outputs the date correctly, are you sure theString isn't nil?
I've a webservice which gives back my date in the following way.
Wed Oct 31 11:59:44 +0000 2012
But I want it to give it back in this way
31-10-2012 11:59
I know that it should be done with a NSDateFormatter. But I don't now how to implement it in the correct way.
I've something like this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT+0:00"]];
NSDate *date = [dateFormatter dateFromString:[genkInfo objectForKey:DATE]];
Can anybody help me?
Kind regards.
Code at the moment
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *date2 = [f2 stringFromDate:date];
Webservice layout
"text": "KRC Genk | Zaterdag is er opnieuw een open stadiontour http://t.co/tSbZ2fYG",
"created_at": "Fri Nov 02 12:49:34 +0000 2012"
Step 1: create an NSDateFormatter to convert your string from server to an NSDate object by setting the format to the format of the "server string"
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
Step 2: create another NSDateFormatter with the desired output string and convert your new NSDate object to a string object using the new NSDateFormatter
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *s = [f2 stringFromDate:date];
desiredformat = s;
P.S. I'm not sure of f format, check this link
http://www.developers-life.com/nsdateformatter-and-uifont.html
There are a few issues with your format string for parsing the original date. And the locale isn't set properly. There is no need to set the timezone. That will be processed from the supplied date/time string.
NSDateFormatter *f = [[NSDateFormatter alloc] init];
NSLocale *posix = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[f setLocale:posix];
[f setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:#"Wed Oct 31 11:59:44 +0000 2012"];
You want to use the en_US_POSIX locale whenever you are parsing (or formatting) a fixed format date that is not from or for a user. Do not use the en_US_POSIX locale to display dates or times to a user.