My date is coming in string format from server
Output of date come like this "07/30/2011",
I want a date format "30 july"
how can i do that?
Easy, use NSDateFormatter
Example,
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"07/30/2011" ];
[dateFormatter setDateFormat:#"dd MMM"];
NSString *output = [dateFormatter stringFromDate:date];
Related
I want to convert date 2012-12-26 to december 26, 2012 in iOS?
I am using websrvice and the data comes in this format 1990-12-26.
I want to change this to december 26, 2012 format.
This is what I am doing:
lbl_Rightside.text = [rootElement stringValueForNode:#"date"];
NSLog(#"lbl_Rightside is %#",lbl_Rightside.text);
[lbl_Rightside release];
Getting date to this label on 1990-12-26. Now I want to change date to december 26, 2012 format.
Any hints from experts would be very welcome.
you can use NSDateFormatter to do this kind of things. First
convert your date String to a date object using dateFromString:
method.
from date convert to string you want using stringFromDate: method
Different format strings can be found here.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *orignalDate = [dateFormatter dateFromString:YOUR_ORIGINAL_STRING];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *finalString = [dateFormatter stringFromDate:orignalDate];
[dateFormatter release]; //if not using ARC
Check the official Apple documentation about NSDateFormatter. You should use this class to do this kind of formatting.
by using NSDateFormatter
NSString to NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate convert to NSString:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", strDate);
[dateFormatter release];
Try to look at NSDateFormatter Class,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"]; // this is your input date format
NSDate *newDate = [dateFormatter dateFromString:dateString];//converting string to date object
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy"]; // setting new format
NSLog(#"The date is = %#",[dateFormatter stringFromDate:newDate])
I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code
I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.
I would like to create an NSDate from a string:
NSString *mostRecentMentionMessageTimestamp = [mostRecentMessage valueForKey:#"updated_at"];
NSLog(#"%#",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(#"%#",mostRecentMentionDate);
When I print my string it is:
2011-11-10T07:22:59Z
After I make a date from string and print it, it is (null)
What am I doing wrong?
You need to set the time zone and drop the Z at the end of the date format. Since your date has a Z, it is in UTC. So you can use:
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
[dateFormatter setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss"];
The format is almost correct, use:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
to solve the time zone issue use the solution by #Jef add:
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
Full example code:
NSString *mostRecentMentionMessageTimestamp = #"2011-11-10T07:22:59Z";
NSLog(#"input: %#",mostRecentMentionMessageTimestamp);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone: [NSTimeZone timeZoneWithAbbreviation: #"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSLog(#"output: %#",mostRecentMentionDate);
NSLog output:
input: 2011-11-10T07:22:59Z
output: 2011-11-10 07:22:59 +0000
the string value which you are trying to convert to NSDate is incorrect.
"2011-11-10T07:22:59Z"
it should have a timezone difference value in place of "Z" to make it compatible to date formatter's format.
e.g. 2011-11-10T07:22:59+0430
I need to convert this string in to the date:
02-09-2011 20:54:18
I am trying the `dateFromString` method but every time it is returning (null), what NSDateFormatter should I use?, I have tried
`[formatter setDateFormat:#"yyyy-MM-dd"]` and `[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]`.
Here is my code:
NSString *finalDate = #"02-09-2011 20:54:18";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateString = [formatter dateFromString:finalDate];
NSLog(#"%#",dateString);
Thanks.
You can convert a NSString containing a date to a NSDate using the NSDateFormatter with the following code:
// Your date as a string
NSString *finalDate = #"02-09-2011 20:54:18";
// Prepare an NSDateFormatter to convert to and from the string representation
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// ...using a date format corresponding to your date
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
// Parse the string representation of the date
NSDate *date = [dateFormatter dateFromString:finalDate];
// Write the date back out using the same format
NSLog(#"Month %#",[dateFormatter stringFromDate:date]);
Surely it's easier to search than it is to ask?
How do I convert from NSString to NSDate?