This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 6 years ago.
I am receiving TimeString from server in such format - "2012-04-23T14:00:00.511Z" and for some reason I get nil when trying to convert that into NSDate object by doing following
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss a"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:00.511Z"];
What might be the issue?
Your code should be like,
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: #"2012-04-23T14:00:51Z"];
NSLog(#"%#",myDate);
Related
This question already has answers here:
How to parse an ISO8601 date [duplicate]
(2 answers)
Closed 6 years ago.
I'm trying to parse this date : 2016-04-16T10:00:00-0400 but I don't know what is '-0400' in this case ?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss<?>"];
^^^
NSDate *date = [dateFormat dateFromString:#"2016-04-16T10:00:00-0400"];
Thanks for your help !
use
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
//[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSDate *date = [dateFormat dateFromString:#"2016-04-16T10:00:00-0400"];
NSLog(#"date ==%#",date);
[dateFormat setDateFormat:#"yyyy"];
NSString *finalStr = [dateFormat stringFromDate:date];
NSLog(#"finalStr ==%#",finalStr);
output like
Thanks to #gnasher729 for the tip, here is the Date Field Symbol Table : http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
The answer is for the date 2016-04-16T10:00:00-0400 is :
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
This question already has answers here:
NSDateFormatter with default time zone gives different date than NSDate description method
(2 answers)
Closed 8 years ago.
NSString *str=#"2015-01-01 00:00:00”;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *dtConverted = [dateFormatter dateFromString:str];
if (dtConverted==nil) {
dtConverted=nil;
}
return dtConverted;
if my str=2015-01-01 00:00:00 then date return 2014-12-31 18:30:00 +0000
if my str=0000-00-00 00:00:00 then date return null
Please help me to resolve my problem. Need the same date as I do have in string.
NSString *str= #"2015-01-01 00:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *dtConverted = [dateFormatter dateFromString:str];
if (dtConverted==nil) {
dtConverted=nil;
}
return dtConverted;
its working perfectly fine and if you want 24 hour format use HH:mm:ss instead of hh:mm:ss
This question already has an answer here:
NSDateFormatter Issues
(1 answer)
Closed 8 years ago.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-YYYY"];
NSDate *myTime = [[NSDate alloc] init];
myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
I am converting a stored date(currently hardcoded) into NSDate format so that I can display it and apply operations on it. But myTime is giving wrong output.
2013-12-21 18:30:00 +0000
whereas I am passing 07-22-2014 as string and expecting me to output 07-22-2014.
Do I need some formatting apart from setting it right. ? What is the reason of going wrong.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate* myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
try this
-(NSString *)dateFormater:(NSDate *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd hh:mm:ss";
NSString *strDate=[formatter stringFromDate:date];
formatter = nil;
return strDate;
}
use like this..
NSString *temp=[self dateFormater:[NSDate date]];
NSLog(#"%#",temp);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two NSDate objects
one in this format
yyyy:MM:dd HH:mm:ss
the other one is in
yyyy-MM-dd HH:mm:ss
Is there any difference between them?
I also tried to convert the second format one to the first format, but for some reason, I can get it to the correct NSString but not in NSDate, NSDate always show as yyyy-MM-dd, I cannot find the reason to it.
here is the code i did to convert from - to :
//photo date has this date 2013-07-21 18:02:13
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:photoDate];
dateString = [dateString stringByReplacingOccurrencesOfString:#"-" withString:#":"];
NSDateFormatter *converter = [[NSDateFormatter alloc] init];
[converter setDateFormat:#"yyyy:MM:dd HH:mm:ss"];
NSDate * newPhotoDate = [converter dateFromString:dateString];
// newPhotoDate still has it in 2013-07-21 18:02:13 but dateString actually is in 2013:07:21 18:02:13
NSDate is an date object there is no formatting on the object.Formats comes for getting the string value from NSDate so that we can present in what way we want.What ever format it may be NSDate remains the same
Eg
I/P
NSDate *date=[NSDate date];
NSLog(#"%#",date);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd-MM-yyyy"];
NSString *dateString =[df stringFromDate:date];
NSLog(#"%#",dateString);
[df setDateFormat:#"dd:MM:yyyy"];
dateString =[df stringFromDate:date];
NSLog(#"%#",dateString);
O/P
2013-07-26 12:47:08.727 Trial[4191:11303] 2013-07-26 07:17:08 +0000
2013-07-26 12:47:08.728 Trial[4191:11303] 26-07-2013
2013-07-26 12:47:08.731 Trial[4191:11303] 26:07:2013
You also need to Convert NSString to NSDate;
Such Like..
NSString *dateString = #"2013-7-12 12:22:51";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateFromString = [[NSDate alloc] init];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"Date : %#", date);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting NSString to NSDate (and back again)
I make a request to the flickr api and it returns me the date in the following format
"2013-02-01T06:25:47Z"
How do i convert this into NSDate format??
It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];
You can use below function:
-(NSDate *)getDateFromString:(NSString *)pstrDate
{
NSDateFormatter *df1 = [[[NSDateFormatter alloc] init] autorelease];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *dtPostDate = [df1 dateFromString:pstrDate];
return dtPostDate;
}
Hope, it will be helpful to you.
This function will accept your string date and return the NSDate value.
Cheers!