NSDateFormat dd/MM/yyyy to MM/dd/yyyy iOS [duplicate] - ios

This question already has answers here:
Changing date format on iOS
(2 answers)
Closed 8 years ago.
Was wondering if there is an easy conversion to take a date in the format dd/MM/yyyy and transform it to MM/dd/yyyy to display.
Current code tried is:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
However, date will return nil in this situation. Any help would be appreciated. Thank you.

You'll have to "decode" and then "encode". You're currently trying to parse 28 as a month. Try this:
//in dd/MM/yyyy format string below.
NSString *stringToFormat = #"28/05/1991";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:stringToFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter stringFromDate:date];

Related

What is the correct date format (NSDateFormatter)? [duplicate]

This question already has answers here:
How to parse an ISO8601 date [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to parse this date : 2016-04-16T10:00:00-0400 but I don't know what is '-0400' in this case ?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss<?>"];
^^^
NSDate *date = [dateFormat dateFromString:#"2016-04-16T10:00:00-0400"];
Thanks for your help !
use
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
//[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSDate *date = [dateFormat dateFromString:#"2016-04-16T10:00:00-0400"];
NSLog(#"date ==%#",date);
[dateFormat setDateFormat:#"yyyy"];
NSString *finalStr = [dateFormat stringFromDate:date];
NSLog(#"finalStr ==%#",finalStr);
output like
Thanks to #gnasher729 for the tip, here is the Date Field Symbol Table : http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
The answer is for the date 2016-04-16T10:00:00-0400 is :
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];

Format NSString 2015-05-24T20:10:00.000Z to M/d, h:mma (5/24, 8:00PM) [duplicate]

This question already has answers here:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
(6 answers)
Closed 7 years ago.
I have an NSString #"2015-05-24T20:10:00.000Z", and I am using an NSDateFormatter with the date format #"yyyy-MM-dd'T'HH:mm:ssZ", but it always returns nil.
I am using the following code, but the output is always (null).
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// NSString *input = #"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"]; //iso 8601 format
NSDate *date = [dateFormat dateFromString:[dictScheduleData valueForKey:#"scheduledOn"]]; // coming from the server 2015-05-24T20:10:00.000Z
NSLog(#"Date output: %#", date);
Try this dateFormat
#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
You have to match the string's date format. Use NSDateFormatter, then set the date format to match the string's date format. Then use dateFromString method.
to convert it to (5/24, 8:00PM), you can change NSDateFormatter's dateFormat to "M/d, h:mma", then use NSDateFormatter's method stringFromDate (the date you previously got, which should not be nil if done correctly). Then put that string in UILabel's text.
NSDate *currentTime = [NSDate date];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[timeFormatter setLocale:[NSLocale currentLocale]];
NSString *DateString = [timeFormatter stringFromDate:currentTime];

Date Format for String [duplicate]

This question already has answers here:
NSDateFormatter won't format strange date string
(3 answers)
Closed 9 years ago.
I have a string of the form "2013-05-12T15:38:20+00:00" but I can't work out why my date formatter won't convert it to an NSDate object. I have tried using "yyyy-MM-ddTHH:mm:ss+zz:zz" and a bunch of variations on it but no luck.
What would be the correct date format for this string?
Thanks
The correct date format for this string is "yyyy-MM-dd'T'HH:mm:ssz"
Thanks #Anupdas for pointing me to a similar question.
Use this format
NSDateFormatter *form = [[NSDateFormatter alloc] init];
[form setDateFormat:#"yyyy-MM-dd'T'HH:mm:sszzz"];
NSLog(#"notDate =====> %#",[form dateFromString:#"2013-05-12T15:38:20+00:00"]);
NSDate *notDate = [form dateFromString:[#"" stringByAppendingFormat:#"2013-05-12T15:38:20+00:00"]];
NSLog(#"notDate =====> %#",[form dateFromString:#"2013-05-12T15:38:20+00:00"]);
I hope it helps you
Try this code
your string
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss+zz:zz"];
NSDate* date = [dateFormatter dateFromString:dateString];
Conversion of the NSString to a NSDate
Now here you change any format as you want
[dateFormatter setDateFormat:#"dddd - MMMM dd,yyy"];
NSString* myNiceLookingDate = [dateFormatter stringFromDate:date];
[dateFormatter release];

Convert NSString to NSDate [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I make a request to the flickr api and it returns me the date in the following format
"2013-02-01T06:25:47Z"
How do i convert this into NSDate format??
It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter 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];
You can use below function:
-(NSDate *)getDateFromString:(NSString *)pstrDate
{
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dtPostDate = [df1 dateFromString:pstrDate];
return dtPostDate;
}
Hope, it will be helpful to you.
This function will accept your string date and return the NSDate value.
Cheers!

Time format in specific format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
format a NSDate to DDMMYYYY?
Want NSDateformatter for January,2000 or January 2000
I need date and time in following format. How can I get ..
Dec 17,2012 5:30 AM
You can always google for these kind of issues
Try to look at NSDateFormatter Class,
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy hh:mm:ss a"]; //dateFormatter is an object of class NSDateFormatter
NSDate *selected_Date = //Your Date object here;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd,yyy hh:mm:ss a"];
NSString* strDateObj [NSString stringWithFormat:#"%#",[dateFormat stringFromDate:selected_Date]];
NSLog(#"Date %#",)

Resources