This question already has an answer here:
dateformatter problem?
(1 answer)
Closed 8 years ago.
I have a time string, which is as shown below:
2/5/2013 12:00:00 AM
and i want to change this in to MM/dd/yyyy formate.
i used NSDateFormatter, but i am getting a null value. how can i change this??
check your code, must be doing something wrong, this is how you receive the right format..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [formatter dateFromString:str];
Read this document for date formatters.
Related
This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 9 years ago.
I am converting the string into date with the help of below code :
NSDateFormatter *fDateFormat = [[NSDateFormatter alloc] init];
[fDateFormat setDateFormat:#"dd/MM/yyyy hh:mm a"];
[fDateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *dateFormat = [fDateFormat dateFromString:strDate];
And passing string like "02/09/2013 05:10 AM" and I will get date perfect. But when we use time format 24 hours, then date is nil.
Can you please me out of this problem ?
NSDateFormatter *fDateFormat = [[NSDateFormatter alloc] init];
[fDateFormat setDateFormat:#"dd/MM/yyyy hh:mm a"];
[fDateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *dateFormat = [fDateFormat dateFromString:strDate];
[fDateFormat setDateFormat:#"dd/MM/yyyy HH:mm"];
NSString *your24Format = [fDateFormat stringFromDate:dateFormat];
You can try above code, should be working fine :)
Wayne
This question already has an answer here:
Convert NSDate to mm/dd/yyyy
(1 answer)
Closed 9 years ago.
I want NSDate in dd/MM/yyyy format, without its time i.e. hh:mm:ss format. I can get string in this format but not nsdate. How to get nsdate like 27/12/2013?
Thanks in advance!
You can use NSDateFormatter to format NSDate variables. In your case you would use it like this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yyyy";
NSLog(#"my date is %#",[dateFormatter stringFromDate:yourNSDateVariable]);
NSFormatter does this very well and easily:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSString *dateInString = [formatter stringFromDate:yourdate];
You can not "format" an NSDate, but instead only a string representation of it (which you say that you already know how to do). Take a look at the NSDate class reference for more details: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/
NSDateFormatter *oldFormatter = [[NSDateFormatter alloc] init];
[oldFormatter setDateFormat: #"dd/MM/yyyy HH:mm:ss"];
NSString *oldString = #"27/12/2013 21:30:21";
NSDate *theDate= [oldFormatter dateFromString:oldString];
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *yourString = [newFormatter stringFromDate:theDate];
NSLog(#"%#", yourString);
This question already has answers here:
NSDateFormatter not working properly?
(2 answers)
Closed 9 years ago.
I have this NSDate:
2013-03-14 17:35:00 +0000
And I try to convert it to NSString with this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
[formatter stringFromDate:item.startdate];
And what I get is:
03/14/2013 05:35:00
How can I fix it to give me the date with the time as in the date?
Use HH instead of hh in your time and it is good idea to set the timeZone also
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
[timeFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *newTime = [timeFormatter stringFromDate:[[NSDate alloc]init] ];
I think you want HH for 24-hour clock:
[formatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
However I noticed this in the Data Formatting Guide which might affect you:
In iOS, the user can override the default AM/PM versus 24-hour time
setting. This may cause NSDateFormatter to rewrite the format string
you set.
[formatter setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
Use H instead of h to get the 24-hour formatted time.
This question already has answers here:
Convert string to date in my iPhone app
(4 answers)
Closed 10 years ago.
How to convert a string to date? I know we can use dateFormatter to do this but I am stuck in between. I have format 0512 which should be converted to May 2012.
Try this,
NSString *finalDate = #"0512";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMyy"];
NSDate *date = [dateFormatter dateFromString:finalDate];
[dateFormatter setDateFormat:#"MMM yyyy"];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
Try this
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMyy"];
NSDate *dateObj= [dateFormatter dateFromString:#"0512"];
NSLog(#"%#",dateObj);
[dateFormatter setDateFormat:#"EEEE MMMM d, YYYY"];
NSLog(#"%#",[dateFormatter stringFromDate:dateObj]);
Try this code may be helped you..
NSString *dateStr = #"0512";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"MMMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",dateStr);
What is so difficult about it?
1. Take out first 2 characters of string
2. Put if else/ convert string to int and use switch to make up month
3. Take the next 2 characters, add them to 2000 and you get year
Now user iOS functions to achieve this :)
This question already has answers here:
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
(8 answers)
Closed 9 years ago.
Am receiving date in this format (2012-07-13 09:22:22 +0000). I want to convert this date format to 13-Jul-2012 09:22. And also if the date is today means i want to change Today 09:22 and Yesterday 09:22. Can anyone please help to do this?
EDIT:
I have added some code what i have tried.
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"dd-mm-yyyy hh:mm"];
[dateFormatter1 setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormatter1 dateFromString:dateStr];// Change Date Format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDoesRelativeDateFormatting:YES];
NSString *dateString1 = [formatter stringFromDate:date]; // Getting Today or Tomorrow
[formatter release];
NSLog(#"DateString1 : %#", dateString1);
First problem I see is that the date pattern your supplying to the Formatter does not match the date string you claim your getting. You might want to try this:
[dateFormatter1 setDateFormat:#"yyyy-mm-dd hh:mm:ss Z"];
The format your using in the code you supplied has year, month, day reversed and your not converting second.
You can try this:
NSDate *date = [NSDate date]; // date in this format 2012-07-14 17:32:09 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm"];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; // Optional
NSString *dateStr = [dateFormatter stringFromDate:date];
And next compare your 'date' with current date using NSDate methods for eg. timeIntervalSinceReferenceDate and then based on the result display today/tomorrow.