iPhone: How to convert "yyyyMMddThhmmss" format string to NSDate? - iphone

I have a string like "20121124T103000" and I want to convert this to NSDate.
I tried converting it with dateFormatter date format "yyyyMMddThhmmss" but it gives output as 2001-01-01 00:00:00 +0000 which is incorrect.
What is the way we can convert this string to NSDate?
Here is my code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMddThhmmss"];
NSLog(#"%#",[dateFormat dateFromString:#"20121124T103000"]);
Any help is appreciated.

Your original code is quite close; instead of:
#"yyyyMMddThhmmss"
You should be using:
#"yyyyMMdd'T'hhmmss"
The only difference is the pair of single quotes around the 'T' in the string- you just need to let the app know that 'T' is a part of the formatting, not the actual date.

do like this,
NSString *dateStr = #"20121124T103000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd'T'hhmmss"];
NSDate *d = [dateFormat dateFromString:dateStr];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st);

Try the following Code.
NSString *dateStr = #"20121124T103000";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"yyyyMMdd'T'hhmmss"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd'T'hhmmss"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st]);

Related

NSDateformatter does not format correctly

I have an NSDateformatter that is not working correctly. I got my date as a string like this.
2013-02-20 00:00:00
And I need to return it as an NSDate like this
2013-02-20
What I've got at the moment is this.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat dateFromString:dateString]
But this is not working.
Any help ?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate* date=[dateFormat dateFromString: dateString];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",[dateFormat stringFromDate: date]);
This will work
try it
if you have more than on date to parse, it could be worthy to create one input and one output date formatter
static NSDateFormatter *inputDateFormatter;
static NSDateFormatter *outputDateFormatter;
if (!inputDateFormatter) {
inputDateFormatter= [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:#"yyyy-MM-dd"];
}
NSDate *date = [inputDateFormatter dateFromString:#"2013-02-20 00:00:00"];
NSString *string = [outputDateFormatter stringFromDate:date];
NSLog(#"%#", string);
result
2013-02-20
as NSDateFormatter are expensive to create, you should either store them in a singleton or declare them static to create them once for that method.
And I need to return it as an NSDate like this
A date has no format, it is just a point in time. you will have to creta a string with the desired format where needed.

String to a Format yyyy-MM-dd HH:mm:ss Iphone

I have an nsstring ,see below
NSString *Mydate=#"9/8/2011"; in month/day/year format.
I want this string to be in the format yyyy-MM-dd HH:mm:ss.
for eg: 2011-09-08 15:51:57
so that i need to show the string in a label in the later format.
Thanks all.
Try below code with valid input string that will help you.
NSString *dateStr = #"9/8/2011 11:10:9";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"d/M/yyyy hh:mm:s"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:s"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st);
[dtF release];
[dateFormat release];
You may use NSDateFormatter

Date Conversion in objective c?

I have an array containing the String objects with this format 22/04/2011.My question is that how can I change this string into the yyyy-mm-dd format. I am using NSDateFormatter but it's not working.
The real thing to know that when you convert a string date to NSDate using NSDateFormatter using any of the above mentioned methods, you will always get the NSDate object in Local TimeZone. So when coding the method for date conversion you need to consider this factor.
Hope this helps..
-:samfisher
Try this,
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:#"22/04/2011"];
[formater setDateFormat:#"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"Date - %#",result);
And For Reference - http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Here how I do this ... This code works ... Try this
NSDate* currentDate = [NSDate date];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-mm-dd"];
NSString* currentDateString = [df stringFromDate:currentDate];
[df release];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];

How can I convert string into date?

I have two strings:
date1 = 3-5-2014;
date2 = 4-2-2010;
I have to convert them in date and then compare them. I want the same format of date as in strings i.e., dd-mm-yyyy. How it can be done?
NSString *string=#"03-05-2014";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:string];
enjoy...
You can extract NSDate objects from strings using the NSDateFormatter class. See the Date Formatters section Apple's Data Formatting Guide for a detailed explanation and sample code.
Try with below
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
here is the SO post
Convert NSString date to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [dateFormatter dateFromString:#"25-8-2010"];
NSLog(#"%#", date);
[dateFormatter release];
Hope this works for you
Cheers :)

NSDateFormatter and yyyy-MM-dd

I'm trying to take a NSString date in the format "2/22/11" and convert it to this format: 2011-02-22
This is my code:
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat dateFromString:newInvoice.date];
newInvoice.date = [dateFormat stringFromDate:dateTemp];
newInvoice.date starts as an NSString equal to "2/22/11". dateTemp ends up NIL. newInvoice.date ends up NIL as well.
I can't for the life of me figure out why.
You are facing this problem because your date formatter is not correct.Suppose your newInvoice.date variable store "11:02:23" the your dateFormatter should be #"yy:MM:dd" and if your newInvoice.date variable store"2/22/11" then your dateFormatter should be #"MM/dd/yy"
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yy"];
[dateFormat2 setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
newInvoice.date = [dateFormat2 stringFromDate:dateTemp];
both format should be according to your requirement to get the correct result
This should work fine. I have set Date style. You can change NSDateFormatterShortStyle to something else. Also check your newInvoice.date format.
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle: NSDateFormatterShortStyle];
dateTemp = [dateFormat dateFromString: newInvoice.date];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
newInvoice.date = [dateFormat stringFromDate:dateTemp];