Convert NSString date to NSDate or a specific string - iphone

Could you please help me with such converting
I have NSString date like #"2/8/2012 7:21:09 PM"
And I need to have such string in output:
"at 7:21 PM, february 8"
I've tried to use dateFormatter with different date patterns but it always return me null..I really don't understandd where I'm doing wrong :(
NSString *dateString = newsItem.Date;//Which is equal to #"2/8/2012 7:21:09 PM"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm/dd/yyyy hh:mm:ss"];//I'm sure that this patternt is wrong but have no idea how to write the right one
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSLog(#"date:%#", dateFromString);

You'll need to use two different date formatters. First one to convert the string in to a date, the second one to output the date as a string with the specified format.
NSString* dateString = #"2/8/2012 7:21:09 PM";
NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[firstDateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* date = [firstDateFormatter dateFromString:dateString];
NSLog(#"Date = %#",date);
NSDateFormatter* secondDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[secondDateFormatter setDateFormat:#"h:mm a, MMMM d"];
NSString* secondDateString = [NSString stringWithFormat:#"at %#",[secondDateFormatter stringFromDate:date]];
NSLog(#"%#",secondDateString);
The trick is the date format strings. They use a format called the unicode date format, the specification can be found here.

Related

Getting date in specified format [duplicate]

This question already has answers here:
Passing a formatted NSDate
(3 answers)
Closed 8 years ago.
I am getting current date in format "2013-02-10 09:26:52 +0000" by using [NSDate date].
I want date in format like "Feb 10".
Can anyone suggest correct dateFormat?
Use NSDateFormatter class:
NSDateFormatter * myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"ddMMyy" options:0 locale:[NSLocale currentLocale]];
NSString * formattedDate = [myFormatter stringFromDate:[NSDate date]];
Here you have info to choose the output format for setDateFormat: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
Hi set the date formatter like this
[dateFormatter setDateFormat:#"MMM d, yyyy hh:mm:ss"];
hope it will work
It is working in my case :
NSDateFormatter* dateFormatterBegin = [[NSDateFormatter alloc] init];
[dateFormatterBegin setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
NSString *dateStringBegin = #"2013-02-10 09:26:52 +0000";
NSDate* datebegin = [dateFormatterBegin dateFromString:[dateStringBegin stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:NSMakeRange(dateStringBegin.length-3, 1)]];
[dateFormatterBegin setDateFormat:#"MMM-dd"];
NSString *beginDate = [[NSString alloc] initWithFormat:#"%s%#","Start Date : ",[dateFormatterBegin stringFromDate:datebegin]];
NSLog(#"Your date is : %#",beginDate);
[dateFormatterBegin release];
[beginDate release];
NSString *string = #"2013-02-10 09:26:52 +0000"; // The date in a string
//Convert the string to date with the same format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Set format
NSDate *date = [dateFormat dateFromString:string];
//Create the new date format using NSDateFormatter
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MMM dd"]; // Set format
//Convert the date using dateFormat1
NSDate *formatDate= [dateFormat1 dateFromString:date]; // date with MMM dd

How to convert Date like "2012-12-26" to "december 26, 2012" in iOS ?

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

Parsing custom string gives wrong date

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

Using NSDate formatter, how to display day of selected date?

Output format: date and day format is in the form of 05 March 2012, MondayNSDate *selected = [picker date];
In this code I got output in the form of 1989-07-03, I need to print the corresponding day.
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"yyyy-MM-dd"];
// [formatter setDateFormat:#"dd %b YYYY, %a"];
//Get the string date
NSString* date = [formatter stringFromDate:selected];
//Display on the console
NSLog(#"%#",date);
Use this:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd %b YYYY, %a"];
NSString *theString = [formatter stringFromDate:theDate];
%b is the name of the month, %a is the name of the day
More info here:
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
dd MM yyyy,EEE like this ?
you can see more info about dateformatter Date_Format_Patterns
use this
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMMM yyyy,EEEE"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
[formatter release];
Instead of using a fixed format, you should chose a style which will be localized:
[format setDateStyle:NSDateFormatterFullStyle];
This way the user will see the date in the date format that he's used to without you needing to worry about it. For example, Americans would get dates like "Tuesday, April 12, 1952 AD" while Germans would get
"Dienstag, 12. April 1952".
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd MMMM yyyy"];
NSLog(#"%#",[formatter stringFromDate:[NSDate date]]);
[formatter release];

How to get date from a string (iPhone)?

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?