I have a string date with this format:
NSString *dateStr = #"2014-04-11T16:46:58Z"
I need to transform this to NSDate.
I have tried this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ssZ"];
also changed the second line to
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
but when I do
NSDate *date = [dateFormatter dateFromString:dateStr];
I obtain nil. Any clues?
Try this:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
It worked for me ;)
Hey I'm having problems formatting my date ....
my code it this :
NSString *dateString = news.date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.DateFormat =#"yyyy-MM-dd'T'HH:mm:ss";
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString: dateString];
NSLog(#" %#", dateFromString);
if i look at my dateString it´s 2012-03-06T12:15:45
but when i format it, it comes out 2012-03-06 11:15:45 +0000
how do i remove the +0000 ???
dateFromString is an NSDate instance. So 2012-03-06 11:15:45 +0000 is the correct result you get when you call NSLog(#" %#", dateFromString);. That's simply how the method description of NSDate works.
If you want to change a string of type 2012-03-06T12:15:45 to a string of type 2012-03-06 11:15:45, then you have to create another string fromdateFromString` like the following:
// ...
dateFromString = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSString *newDateString = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", newDateString);
This prints the selected datePicker's date to a textField like this:
February 9, 2012 ...
NSDate *theDate = [calendar dateValue];
if (theDate)
{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *formattedDateString;
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
formattedDateString = [formatter stringFromDate:theDate];
[dateString setStringValue: formattedDateString];
}
}
How do I format the printed string to look like this?
2012-02-09
I tried adding a dateFormatter to the textField and customizing how it should display a date. Doesn't work.
Thanks for the help.
You can do it like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *theDate = [calendar dateValue];
NSString *formatedDateString = [formatter stringFromDate:theDate];
[formatter release];
I have the data as customerFromDate " 01 Apr 2010 " and customerToDate " 30 Apr 2010 " which is a string.
I want to convert that format into the string "yyyy-MM-dd", but when doing so I got null values.
Please see the following code which I had tried.
printf("\n customerFromDate %s",[customerStatementObj.customerFromDate UTF8String]);
printf("\n customerToDate %s",[customerStatementObj.customerToDate UTF8String]);
/*
prints as the following
customerFromDate 01 Apr 2010
customerToDate 30 Apr 2010
*/
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *fromDate=[[NSDate alloc]init];
fromDate = [dateFormatter dateFromString:customerStatementObj.customerFromDate];
printf("\n fromDate: %s",[fromDate.description UTF8String]);
NSString *fromDateString=[dateFormatter stringFromDate:fromDate];
printf("\n fromDateString: %s",[fromDateString UTF8String]);
[dateFormatter release];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd"];
NSDate *toDate=[[NSDate alloc]init];
toDate = [dateFormatter1 dateFromString:customerStatementObj.customerToDate];
printf("\n toDate: %s",[toDate.description UTF8String]);
NSString *toDateString=[dateFormatter1 stringFromDate:toDate];
printf("\n toDateString: %s",[toDateString UTF8String]);
[dateFormatter1 release];
Thank you,
Madan Mohan.
Several notes:
You need two different NSDateFormatters. One that specifies the input date format, and one that specifies the output date format.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd MMM yyyy"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"yyyy-MM-dd"];
You can reuse these formatters for both your fromDate and your toDate.
Secondly, dateFromString: returns an allocated, autoreleased NSDate object. You are leaking the ones you manually allocate.
#import <Cocoa/Cocoa.h>
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd MMM yyyy"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *fromDate = [inputFormatter dateFromString:#"01 Apr 2010"];
NSDate *toDate = [inputFormatter dateFromString:#"30 Apr 2010"];
printf("\n fromDate: %s",[fromDate.description UTF8String]);
NSString *fromDateString=[outputFormatter stringFromDate:fromDate];
printf("\n fromDateString: %s",[fromDateString UTF8String]);
printf("\n toDate: %s",[toDate.description UTF8String]);
NSString *toDateString=[outputFormatter stringFromDate:toDate];
printf("\n toDateString: %s",[toDateString UTF8String]);
[inputFormatter release];
[outputFormatter release];
[pool drain];
return 0;
}
I basically need to get current date and time separately, formatted as:
2009-04-26
11:06:54
The code below, from another question on the same topic, generates
now: |2009-06-01 23:18:23 +0100|
dateString: |Jun 01, 2009 23:18|
parsed: |2009-06-01 23:18:00 +0100|
This is almost what I'm looking for, but I want to separate the day and time.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"MMM dd, yyyy"];
NSDate *parsed = [inFormat dateFromString:dateString];
NSLog(#"\n"
"now: |%#| \n"
"dateString: |%#| \n"
"parsed: |%#|", now, dateString, parsed);
this is what i used:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
[dateFormat release];
[timeFormat release];
[now release];
iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.
And of course don't forget to release your date formatters when you're done with them. The above code leaks format, now, and inFormat.
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
set the format to return is....
yyyy-MM-dd return 2015-12-17 date
yyyy-MMM-dd return 2015-Dec-17 date
yy-MM-dd return 15-12-17 date
dd-MM-yy return 17-12-15 date
dd-MM-yyyy return 17-12-2015 date
yyyy-MMM-dd HH:mm:ss return 2015-Dec-17 08:07:13 date and time
yyyy-MMM-dd HH:mm return 2015-Dec-17 08:07 date and time
For more Details Data and Time Format for Click Now.
Thank you.....
you can use this method just pass your date to it
-(NSString *)getDateFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"your current date format"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"your desired format"];
NSString *stringFromDate = [formatter stringFromDate:myDate];
NSLog(#"%#", stringFromDate);
return stringFromDate;
}
nothing new but still want to share my method:
+(NSString*) getDateStringFromSrcFormat:(NSString *) srcFormat destFormat:(NSString *)
destFormat scrString:(NSString *) srcString
{
NSString *dateString = srcString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:#"MM-dd-yyyy"];
[dateFormatter setDateFormat:srcFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
//[dateFormatter setDateFormat:#"yyyy-MM-dd"];
[dateFormatter setDateFormat:destFormat];
NSString *newDateString = [dateFormatter stringFromDate:date];
return newDateString;
}
For swift
var dateString:String = "2014-05-20";
var dateFmt = NSDateFormatter()
// the format you want
dateFmt.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt.dateFromString(dateString)!;
Swift 3
extension Date {
func toString(template: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: NSLocale.current)
return formatter.string(from: self)
}
}
Usage
let now = Date()
let nowStr0 = now.toString(template: "EEEEdMMM") // Tuesday, May 9
let nowStr1 = now.toString(template: "yyyy-MM-dd") // 2017-05-09
let nowStr2 = now.toString(template: "HH:mm:ss") // 17:47:09
Play with template to match your needs. Examples and doc here to help you build the template you need.
Note
You may want to cache your DateFormatter if you plan to use it in TableView for instance.
To give an idea, looping over 1000 dates took me 0.5 sec using the above toString(template: String) function, compared to 0.05 sec using myFormatter.string(from: Date).
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"]
NSString *dateString = [df stringFromDate:date];
[df setDateFormat:#"hh:mm:ss"];
NSString *hoursString = [df stringFromDate:date];
Thats it, you got it all you want.