Need help in NSDateFormatter - date

I know this is a simple question, but im dealing with this problem for almost 6 hours. I want to order a date to another format. Not a big deal, but it still gives me back: (null)
Example:
Date I have:
2013-06-01 10:54:42
Date I want to be displayed:
01-06-2013 10:54:42
My code:
NSString *dateString = #"2013-06-01 10:54:42";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"%#",date);
Any help would be appreciated!

Try:
NSLog(#"%#",[dateFormat stringFromDate:date]);
You are creating the formatter but not using it to create your date string. Instead you're just printing the date object directly and its description method is printing it out with a default formatting, not the formatter you're trying to use.

Your code works fine
NSString *dateString = #"2013-06-01 10:54:42";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormat stringFromDate:date]);

Either way it works for me. Must be something in the actual code you have the problem with. The code you show is obviously prepared for this question:
NSString *dateString = #"2013-06-01 10:54:42";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormat stringFromDate:date]);
Output:
2013-06-01 19:54:19.497 testpgm[23606:c07] 2013-06-01 08:54:42 +0000
2013-06-01 19:54:21.452 testpgm[23606:c07] 01-06-2013 10:54:42

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.

NSDate and time interval issues

I am trying to convert a NSString into a double using the following:
double lastSynced;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:#"2012-03-28 18:41:25"];
lastSynced = [dateFromString timeIntervalSince1970];
However when printing lastSynced it always gives me 0. Why is this?
Your date format is wrong. Should be:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
You have to do:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
instead of:
[dateFormatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
Good luck!
Your formatting strings are not correct. You should use :
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Good luck !

Objective-C String(yyyy-mm-dd) to NSDate

My App is a JSON code which involves a date in string type. (Such as "2011-10-01").
I would like to know how I could conver it into NSDate?
It needs to be displayed in a different time format such as "1 October, 2011".
ex. this code doesn't work:
NSString *date1 = #"2010-11-12";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MMMM-dd"];
NSDate *date2 = [dateFormat dateFromString:date1];
NSString *strdate = [dateFormat stringFromDate:date2];
As Tug writes in his blog post on the subject:
Objective-C and iOS SDK provide a class to help formatting date
(marshaling and unmarshaling), this class is NSDateFormatter. No
surprise, the NSDateFormatter uses the Unicode Date Format
Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:publicationDate ];
[dateFormatter release];
where publicationDate in this case is an NSString.
Use NSDateFormatter. The appropriate method is dateFromString:. Take a look at the documentation :)
You can try this
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd MM yyyy"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSDate *someDate=[NSDate date];
NSString *targetDateString = [df stringFromDate:someDate];
NSLog(#"date:%#",targetDateString);

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 :)

Formatting a Date String using NSDateFormatter

I have a NSString which is passed from an xml feed........
NSString *strDate =#"May 14 2010";
I'm currently using this code to format the date........
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateFormat:#"d:MMM"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
I want to format my string only to display day and month and currently I'm getting a nil value to the NSDate. Can anyone please help me with this?......
Thanks.
I think you're slightly misunderstanding how NSDateFormatter works. It cannot automatically work out what format the date in your string is in.
If you change your code to something like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd yyyy"];
NSDate *myDate = [dateFormatter dateFromString:strDate];
[dateFormatter setDateFormat:#"d:MMM"];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
It should do what you want.