NSDate set format to MM/YYYY but result shows dd/YYYY - ios

I have a sample code as below and I would like to show in MM/yyyy. However, I get result in dd/yyyy.
NSString *strJoinedDate = [prefs stringForKey:#"creation_date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss a"];
NSDate *JoinedDate = [dateFormatter dateFromString:strJoinedDate];
[dateFormatter setDateFormat:#"MM/yyyy"];
NSString *strFinalJoinedDate =[NSString stringWithFormat:#"Since %#",[dateFormatter stringFromDate:JoinedDate]];

You have the wrong date format for a date string such as 5/29/2018 3:23:28 AM. Change:
dd/MM/yyyy HH:mm:ss a
to:
MM/dd/yyyy hh:mm:ss a
You also need to set the date formatter's locale to en_US_POSIX.
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];

Related

iOS Date format 28 Mar 2016 / 16:54pm

I want to display the date in the format "28 Mar 2016 / 16:54pm" from the string "2017-05-05T08:27:31.337Z". How can I do this? Is there any buildin method for iOS? Thanks.
use NSDateFormatter for your concept
for e.g
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
NSString *final = [dateFormatter stringFromDate:msgDate];
update
add setAMSymbol and setPMSymbol in your dateFormatter.
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
Full Answer
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:#"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];
[dateFormatter setAMSymbol:#"am"];
[dateFormatter setPMSymbol:#"pm"];
NSString *final = [dateFormatter stringFromDate:msgDate];
See Instruction or introduction with every line
NSString *str = #"your date here..."; /// here this is your date with format yyyyy-MM-dd'T'HH:mm:ss.SSSZ
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:#"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy / HH:mma"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(#"Converted String : %#",convertedString);

coversion of date and time from 31 May 2016 04:30 PM(current format) to 2016-05-31T16:30:00.000+05:30(Required format)

hi can any on help me in achiving this is the string which i have 31 May 2016 04:30 PM(NSSTring) 2016-05-31T16:30:00.000+05:30(Required Format)
NSString *dateString = dateandtime;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
used the following code but returns nil
The current format string does not contain any time zone information, so you have to set the time zone in the date formatter as well as the locale to be able to parse the string independently from the current locale.
The input format is dd MMM yyyy hh:mm a
NSString *dateString = #"31 May 2016 04:30 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:19800];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
Some how I tried and worked
NSString *myString = dateandtime;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd MMM yyyy hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ";
NSLog(#"%#",[dateFormatter stringFromDate:yourDate]);
Whatever you do, test that your code works if the user doesn't use an Indian timezone, and if the user uses 24 hour time instead of AM/PM on their phone. I assume your "dateandtime" was created by converting an NSDate to an NSString; it would be much better to not do that but start with the original NSDate.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = #"2016-05-31T16:30:00.000+05:30";
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(#"CurrentDate:%#", currentDate);
you can use this code.

Converting a NSString to NSDate

I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];

Date Format changing while printing description

I am getting some date in this format "04/11/13 19:37:00" I want to convert this date to local time zone, Below is the code i am using.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"MM/dd/yy HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"date value %#",date);
The Problem i am facing is i am using debugger when i place mouse on the "date" variable it is showing "2013-04-12 01:07:00 IST" This is the correct out put i need. But when i am printing the same "date" variable using NSLog i am getting "2013-04-11 19:37:00 +0000". How can i get the correct format can any one explay, Do i need to change the dateformat.
NSLog shows NSDate objects in a fixed format. If you want to log the date in a specific format, convert the NSDate to an NSString using an NSDateFormatter and then log the string.
Remember, logging a date is only for debugging purposes. The format in the log really doesn't matter.
If you want to convert the dateString which was in UTC to Local Timezone, you need to do it in two steps.
1.First form the date by setting the corresponding TimeZone to dateFormatter
2.Set the Local TimeZone to dateFormatter, and form dateString from date
NSString *dateString = #"04/16/13 11:12:00";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
dateFormatter.dateFormat = #"MM/dd/yy HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate* date = [dateFormatter dateFromString:dateString];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(#"%#",localDateString);

Trying to change the date format on iOS

I parse a date from an xml file and store it in a string variable like this :
NSString *dateTex = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
When i try to print this variable on console i get :
the object value is:Fri, 01 Jun 2012 15:52:00 GMT
I am trying to change the format of the above date , to be stored again in a string variable but like this : dd/mm/yy.
I tried this code :
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
But when i print the dateText variable on console i get :
the object value is:(null)
What am i doing wrong and the date is never stored in the variable?
Thanks!
Try this,(setDateFormat line is changed).
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"]; // changed line in your code
NSDate *date = [dateFormatter dateFromString:dateTex];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MM/yyyy"]; // changed line in your code
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(#"the object value is:%#",dateText);
For Calender year use: 'yyyy' and for month use: 'MM'
Probably you parsing format is wrong. Check, if NSDate* date is filled correctly.
Perhaps you need another d, because you get the day with a leading zero.
Try: EEE, dd MMM YYYY HH:mm:ss Z.
Your date format for the first dateformatter is wrong. It should be
[dateFormatter setDateFormat:#"EEE, dd MMM YYYY HH:mm:ss Z"];
Hope this helps.

Resources