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
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am stuck for a while parsing date in string using NSDateFormatter
Date in string is in below format
1/1/2011 12:00:00 AM
Below is the code I use to parse
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSDate *date = [[NSDate alloc]init];
date = [dateFormatter dateFromString:#"1/1/2011 12:00:00 AM"];
This code works with date #"12/31/2011" and format #"MM/dd/yyyy"
Please help
use my bellow custom method and parse that date into another format...
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
//NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
use this method like bellow..
NSString *strSDate = [self changeDateFormat:#"1/1/2011 12:00:00 AM" dateFormat:#"dd/MM/yyyy hh:mm:ss a" getwithFormat:#"dd/MM/yyyy"];
NSLog(#"Converted String : %#",strSDate);
Set any Format which you want in getwithFormat parameter and set format in dateFormat parameter which you have with your Date string...
OUTPUT IS => Converted String : 01/01/2011
Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
//First format date from string
NSDate *date = [dateFormatter dateFromString:#"01/01/2011 12:00:00 AM"];
//Set date formatter to specific format
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
//Log the value
NSLog(#"%#",[dateFormatter stringFromDate:date]);
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 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.
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.