NSDateFormatter does not formate date correctly ios10 - ios

I am trying to convert date with specific formate and just wanted to get date from passed date as shown below but getting nil
What am I doing wrong?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *date = [dateFormatter dateFromString: [dicValues valueForKey:#"DateNeeded"]];
[dateFormatter setDateFormat:#"dd/MM/yyyy h:mm a"];
NSString *strSubTitle = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:date]];
Date i am receiving is : 2016-11-17T23:46:50.35 , 2016-11-28T21:25:20.927 and 2016-11-29T00:00:00

You need to convert
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
to this
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
As in your date string you are receiving MilliSeconds also
Edit
This code is working for me, check the date format which you are getting from server
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormatter dateFromString: #"2016-11-28T21:25:20.927"];
if (date == nil) {
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
date = [dateFormatter dateFromString: #"2016-11-28T21:25:20.927"];
}
[dateFormatter setDateFormat:#"dd/MM/yyyy h:mm a"];
NSString *strSubTitle = [[NSString alloc] initWithFormat:#"%#", [dateFormatter stringFromDate:date]];

[dicValues valueForKey: #"DateNeeded"] is either nil or does not match the specified format "yyyy-MM-dd HH:mm:ss".

First check that date you receive, is in yyyy-MM-dd HH:mm:ss format. If not then make this format according to date you received.
For Example, you received date 29/11/16, then your code will be like this
[dateFormatter setDateFormat:#"dd/MM/yy"];
NSDate *date = [dateFormatter dateFromString: [dicValues valueForKey:#"DateNeeded"]];
Once you get date, then convert it any format you want.

try
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];

Related

Unable to get date from string?

I Get DateTime string from server "8/27/2016 6:56:23 AM"
And I want to fetch only date from this string.
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
But the problem is I am getting null in date1.
please help me to find out where I am doing wrong.
You are using the wrong dateFormat. Please replace your code with following:
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
do like
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"]; or use hh:mm:ss a
NSDate *date1=[dateFormatter dateFromString:StreamStartDateTime];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSString *ssecond=[dateFormatter stringFromDate:date1];
NSDate *finalDate=[dateFormatter dateFromString:ssecond];
final output as
Your date format must be match with your date string.
So If your date string is 8/27/2016 6:56:23 AM then your format must be MM/dd/yyyy hh:mm:ss a. Then with this format get date from string first like,
NSDate *date=[df dateFromString:StreamStartDateTime];
Now, change date formatter in whatever format you want your date say,
[df setDateFormat:#"yyyyMMdd"];
then
NSString *resultDateStr = [df stringFromDate:date];
Can try like this
NSString *StreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *streamStartDate = [formatter dateFromString:StreamStartDateTime];
[formatter setDateFormat:#"yyyyMMdd"];
NSString *strDate = [formatter stringFromDate:streamStartDate];
NSDate *compDate = [formatter dateFromString:strDate]; // Get date in UTC format
Output:
Printing description of strDate:
20160827
Printing description of compDate:
2016-08-26 18:30:00 +0000
NSString *LiveStreamStartDateTime=#"8/27/2016 6:56:23 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *date1=[dateFormatter dateFromString:LiveStreamStartDateTime];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fisrtdate=[dateFormatter stringFromDate:date1];
NSDate *date11 = [dateFormatter dateFromString:fisrtdate];

NSDateFormatter for day and month name

I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
try this...
//Getting date from string
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
// converting into our required date format
[dateFormatter setDateFormat:#"EEEE, MMMM dd, yyyy"];
NSString *reqDateString = [dateFormatter stringFromDate:date];
NSLog(#"date is %#", reqDateString);
LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015
Firstly you must convert your dateString to NSDatevalue
NSString *dateString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
after that you could use this format: #"EEEE,MMMM dd, yyyy" to convert that dateValue to dateString like Monday, March 09, 2015
Hope this help
Helpful link for you
Try to set dateFormat property to #"dd'-'MM'-'yyyy" . Always check the unicode the date symbol table. "MMMM" means that the date month should be a full month name.
Try this one..
NSString * yourJSONString = #"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
[dateFormatter setDateFormat:#"EEEE,LLLL dd, yyyy"];
NSString *output = [dateFormatter stringFromDate:dateFromString];
NSLog(#"%#", output);
Try this:-
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:#"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];
NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(#"Formatted date: %#", dateFormatted);

Which dateFormat should I choose while converting NSString to NSDate?

I have a NSString like #"2014-11-27T10:54:08.185Z"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate * date = [dateFormatter dateFromString:self.creationTime];
I tried a lot of different formatter strings, but the date is always nil.
Or is there another problem here?
The hour in your date string format seems to be in the 24h format.
So you need to use HH instead of hh in your dateFormat.
More info
Try
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'hh':'mm':'ss'.'SSS'Z'"];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.creationTime];
self.dateLabel.text = [formatter stringFromDate:date];
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];
Please try this...:)

How to get NSDate in "dd/MM/yyyy" formate ios?

I am working with NSDate i created one function which give Today date with "dd/MM/yyyy" format but NSDate always show "nil" value. I googled lot and also see lots of stackoverflow answer but all answer return NSString value not NSDate value so please guide me to get NSDate with given format.
+(NSDate*)getCurrentDate{
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
//Date print here
NSLog(#"Utility CurrentDate: %#",[DateFormatter stringFromDate:[NSDate date]]);
NSString *dateString = [DateFormatter stringFromDate:[NSDate date]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:DATE_FORMAT];
//Date print here
NSLog(#"New Date: %#",[newDateFormatter stringFromDate:[NSDate date]]);
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
return dateFromString;
}
The issue is here
NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
Here your dateString is 2014-12-11 03:41:31, hence it is not getting formatted. You should change either newDateFormatter or DateFormatter to convert as per your requirement.
EDIT:
Replace [DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; with [DateFormatter setDateFormat: DATE_FORMAT];
But your return date will not be in the desired way, since you are returning an NSDate Object. You should return a formatted string.
You are completely misunderstanding what NSDate is and does. An NSDate is an absolute point in time. It has no format. It cannot have a format. It is absolutely impossible to have an NSDate in "dd/MM/yyyy" format or in any other format.
When you want to display a date to the user, you use NSDateFormatter to convert the NSDate into a string. In any format you like.
check here Convert date from string or you can use this methods to convert.
These all three functions you can use to format date values..
-(NSString*)getStringFromDate:(NSDate*)pDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter stringFromDate:pDate];}
-(NSDate*)getDateFromString:(NSString*)pStrDate withFormat:(NSString*)pDateFormat{
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:pDateFormat];
[dtFormatter setLocale:[NSLocale systemLocale]];
return [dtFormatter dateFromString:pStrDate];}
-(NSString*)dateStringFromString:(NSString *)dateToConver format:(NSString *)fromFormat toFormat:(NSString *)toFormat{
NSDate *date = [Helper getDateFromString:dateToConver withFormat:fromFormat];
return [Helper getStringFromDate:date withFormat:toFormat];}
Refer the below modified method in dd/MM/yyyy format:-
+(NSString*)getCurrentDate{
NSDateFormatter *format=[[NSDateFormatter alloc]init];
[format setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSString *currentDt=[format stringFromDate:[NSDate date]];
NSDate *dt=[format dateFromString:currentDt];
[format setDateFormat:#"dd/MM/yyy"];
return [format stringFromDate:dt];
}

How to change date format from string to dd/mm/yy on iOS?

So i am parsing an xml file and display on screen the dates that i parse.
The dates are like : Thu , 31 May 2012 20:43:54 GMT.
How can i convert the date to this format : dd/mm/yy ?
i dont need the time.
Well it crashes.
I tried :
NSString *date = [[stories objectAtIndex: storyIndex] objectForKey: #"date"];
//changing dates format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:date];
NSString *dateText = [[NSString alloc] initWithString:[dateFormatter stringFromDate:myDate]];
Any ideas?
You can use NSDateFormatter
For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss Z"];
NSDate *myDate = [dateFormatter dateFromString:yourString];
See http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns for dateformat patterns
The idea is to use NSDateFormatter to get a date from your input time string, then use a new dateformatter to get whatever new time string you want from the date.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/mm/yyyy"];
NSString *newDateString = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
Alternatively for the second part You can use NSDateComponents.
after:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, d MMM YYYY HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:theDateString];
[dateFormatter release];
//Get NSComponents from date object..
[NSString stringWithFormat:#"%#/%#/%#",dateComponent.date,dateComponent.month,dateComponent.year];

Resources