date formatter giving wrong dates in iOS? - ios

I had an application in which i am using a date formatter for a string like
"6/30/2016 1:00:00 PM" like this 30-Jun-2016 1:00.I am doing like this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"mm/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"dd-MMM-yyyy hh:mm"];
NSString *stringDate = [dateFormat stringFromDate:date];
return stringDate;
but it is not giving me wrong date like this 30-Jan-2016 1:00 .
can anybody point me where i am going wrong ?

You have given the wrong date format you need to capitalize month MM not small, small mm is for minute. Change your code like this.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:#"dd-MMM-yyyy hh:mm"];
NSString *stringDate = [dateFormat stringFromDate:date];
return stringDate;
Hope this will help you.

Related

NSDateFormatter does not formate date correctly ios10

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"];

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];

How to display date format like “16 Mar,Monday” in iOS? 2

I need to display today date format like 16 Mar,Monday,Here my code is
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM, EEEE"];
dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
I am getting output is 16MAR,MONDAY ,But my output should be 16 Mar, Monday.Thanks
Try this
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM,EEEE"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"Date String %#",dateString);
I am getting same output that you want
Hope you will got success..

how to convert string into date format in iOS?

I have string from web service like 12/31/2013 09:12:15 A.M.
Now I Want convert it like 12 Dec 2013 09:12:15 A.M.
with the use of NSDAteFormatter in iOS
I'm giving you the answer, but downvoting your question for being too elementary and whose solution could easily found with a simple search.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [formatter dateFromString:myDateString];
[formatter setDateFormat:#"dd MMMM yyyy aa:mm:ss a"];
NSString *newDate = [formatter stringFromDate:date];
NSString *dateStr = #"12/31/2013 09:12:15 AM";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];

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