iPhone SDK Date Format [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to change format of date/time?
In my program I'm making an API call for getting a date, but the received date has an incorrect format. It looks like 2011-07-06 00:00:00. How I can change it to 6-july-2011?

You try my code that will help you because I run successfully.
NSString *dateStr = #"2011-07-06 00:00:00";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:#"d-MMM-yyyy"];
NSString *strDate = [dateFormatStr stringFromDate:d];
NSLog(#"%#",strDate);

try this code..
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"dd-MMM-YYYY"];
NSString *dateStr = [formatter stringFromDate:[NSDate date]];

Related

NSDateFormatter returns nil in iphone 5 ios 6.1

Can someone tells me whats wrong with my time formatter? when i used ipod 6.0 the time formatter works. but when i used iphone 5 6.1 the time formatter returns nil.
Here are my codes regarding to the timeFormatter.
NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init] autorelease];
[tempFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *dateandtime = #"2013-05-27 19:00";
//set end date and time
NSDateFormatter *tempFormatterTo = [[[NSDateFormatter alloc]init] autorelease];
[tempFormatterTo setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *dateandtimeTo = #"2013-05-27 20:00";
NSDate *startDate = [tempFormatter dateFromString:dateandtime];
NSDate *endDate = [tempFormatterTo dateFromString:dateandtimeTo];
Thanks in advance. please help me.
Try to use this format. HH used for 24 hour format.
[tempFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
Try This
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate* d = [df dateFromString:#"2013-05-27 19:00"];
NSLog(#"%#", d);
here problem in your Format change it to yyyy-MM-dd HH:mm also use my bellow method when you want to convert any NSString date to NSDate .. This is my custom method just paste this method in your .m class..
-(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:dateFormat];// set format here which format in string date
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
and use above method like bellow...
NSDate *startDate = [self convertStringToDate:#"2013-05-27 19:00" dateFormate:#"yyyy-MM-dd HH:mm"];
NSDate *endDate = [self convertStringToDate:#"2013-05-27 20:00" dateFormate:#"yyyy-MM-dd HH:mm"];

How to Change Date and Time Format? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to change the date format in iphone sdk
Actually I'm getting a string in 24 Hours time format. I want to display 12 hours time format.
Here's my input :
2012-10-19T04:10:00
2012-10-06T14:00:00
I would like to have the following format :
10-19-2012 4:10 AM
10-06-2012 2:00 PM
:use the NSDateFormatter to change the date
Here Your input:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];
And date converted to the String ..
NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:#"mm-dd-yyyy hh:mm a"];
NSString *stringForNewDate = [dateFormatterNew stringFromDate:Yourcurrenttime];
Use NSDateFormatter with format type dd-MM-yyyy hh:mm:ss a.
Hope this helps you..
NSDate *currentDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm a"]; //You can set different formats for the date
NSLog(#"Current Date %#",[formatter getStringFromDate:currentDate"]);
[formatter release];
In most cases, you’d just need to use the default styles defined in NSDateFormatterStyle.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(#"%#", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];
// Output: Dec 2, 2008 3:58 PM
also you can get output from coding with DateFormatter like bellow..
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"MM-dd-yyy HH:mm:s a"];
NSString *convertedString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"Converted String : %#",convertedString);
:)

How to specify the conversion format of a date to a string

I am creating an app where the current date needs to be displayed in a label and automatically updated each day. I have looked at a few tutorials and they have all shown how to get the date in YYYY:DD:MM hh:mm, I am wondering how to change this format to DD/MMM/YYYY.
Any sample code or links to tutorials would be greatly appreciated.
Something like this:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateformatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
[myLabel setText:[formatter stringFromDate:today]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *dateString = [dateFormatter stringFromDate: dayDate];
//display dateString in label
[dateFormatter release];
You need to use the NSDateFormatter. For specfic formats look at
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];

How to parse an ISO8601 date [duplicate]

This question already has answers here:
How to parse a date string into an NSDate object in iOS?
(7 answers)
Closed 9 years ago.
I got string of date regarding this format:
2011-12-29T09:09:06-0500
How can I convert this into a date object?
Try this solution
NSString *dateString = #"2011-12-29T09:09:06-0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSLog(#" dateFromString %#",dateFromString);
NSString *date = [[NSDate date] description];
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];

How change the date format which is stored in string? Objective c [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert datetime format in NSString?
I have stored date in string from json parsing. The format of date is 2011-1-24. Now i want to convert into MM-dd-YYYY format. For that i am using this code
NSString *stringDate =[NSString stringWithFormat:#"%#",[list_date objectAtIndex:indexPath.row]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
NSString *newDate = [dateFormatter stringFromDate:date];
But it show null value. What is problem in this code?
Thanks in advance...
It is different from #Mayur Joshi's answer though it looks to be same.
This is sure to work for you.
NSString *dateString = #"2011-09-19";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [format dateFromString:dateString];
[format setDateFormat:#"MM-dd-yyyy"];
NSString* finalDateString = [format stringFromDate:date];
[format release];
I hope this helps you.
If you need any help on this then please leave a comment below.
Also you can refer to this link for more help. This link is really similar to what you want, only the target date Format is different.
How to convert date string into format "17 Nov 2010"
NSString *dateStr = #"2011-1-24";
//OR if you have date then declare dateStr like this,
NSString *dateStr = [NSString stringWithFormat:#"%#",yourDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-M-dd"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"MM-dd-YYYY"];
NSString* temp = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",temp);