Convert date to string with different date format ios? - ios

I search some stack quentions for converting string to date and that's working fine, but i convert my string date to NSDate than it's return me nil. Here is my code :
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateStr];
I want to convert this date to yyyy-MM-ddTHH:mm:ss.SSSZ this formate.
What's problem here ?

do like
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// set the date format related to what the string already you have
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
// again add the date format what the output u need
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *finalDate = [dateFormat stringFromDate:date];
NSLog(#"finalDate==%#",finalDate);
output

Your problem is that your date format #"yyyy-MM-ddTHH:mm:ss.SSSZ" doesn't match your date string #"2016-07-29 09:05". You need to use a different date format.
Working copy -
Objective C
NSString *dateStr = #"2016-07-29T09:05:00.00000Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormat dateFromString:dateStr];
Swift 2.x
var dateformater = NSDateFormatter()
dateformater.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
var dateString:String = "2016-07-29T09:05:00.00000Z"
let date:NSDate = dateformater.dateFromString(dateString)!
print(date)

Try this code.
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"the date is %#",date);
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSString *currentDate = [dateFormatter stringFromDate:date];
NSDate *finaldate = [dateFormatter dateFromString:currentDate];
NSLog(#"CurrentDate:%#", finaldate);

Just change your formatterstyle as below
NSString *dateStr = #"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
After that you can convert the date into below format :
dateFormat.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
NSString *str = [dateFormat stringFromDate:date];
NSLog(#"%#",str);

Related

Unable to convert NSString to NSdate format

I want to convert my input string to particular NSDate format,
Here is the code I have tried.
NSString * result;
NSDate * resultDate;
NSDate *date = [dateFormat dateFromString:maxUpcomingDateString];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
dateFormat1.locale=[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat1 setDateFormat:#"yyyy/MMM/dd"];
NSLog(#"maxUpcomingDateString %#",maxUpcomingDateString)
NSDate *d=[dateFormat1 dateFromString:maxUpcomingDateString];
result = [dateFormat1 stringFromDate:date];
resultDate=[dateFormat1 dateFromString:result];
NSLog(#"max1 date upcoming %#",result);
NSLog(#"max2 date upcoming %#", resultDate);
Output of my log shows:
maxUpcomingDateString 10 January 2017
max1 date upcoming 2017/Jan/10
max2 date upcoming (null)
I want the output date in the form of 2017/Jan/10. When I try to log the string it shows me the correct output but when I convert it back to NSDate it shows null.
nsstring to nsdate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
nsdate to nsstring
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#", stringDate);
add this code on dateFormat
[dateFormat setDateFormat:#"dd MMM yyyy"];
It will solve the problem.
Add this code after allocating the dateFormat object.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM yyyy"];

What is the Date format

What is the date and time format for "06/08/2016 09:50:12" to be converted to "dd MMMM YYYY HH:mm"?, which i'm converting the NSDate to NSString.
Use this code,
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSString *result = [dateFormat stringFromDate:date];
its working for me, hope its helpful.
Try this code:
NSDateFormatter *format= [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
// string to date
NSString *dateStr = #"06/08/2016 01:50:12";
NSDate *date = [format dateFromString:dateStr];
// date to string
[format setDateFormat:#"dd MMMM yyyy HH:mm"];
NSString *dateStr2 = [format stringFromDate:date];
NSLog(#"%#", dateStr2);
Try this,
NSString *strDate = #"06/08/2016 09:50:12";
NSDateFormatter *format= [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/YYYY HH:mm:s"];
NSDate *date = [format dateFromString:strDate];
NSLog(#"%#",date);
To convert this date into string :
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/YYYY HH:mm:s"];
NSString *str = [format stringFromDate:date];
Hope this help
NSDateFormatter *format= [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MM/YYYY HH:mm:ss"];
// string to date
NSString *dateStr = #"06/08/2016 01:50:12";
NSDate *date = [format dateFromString:dateStr];
NSLog(#"%#", date);
// date to string
NSDate *date2 = [NSDate date];
NSString *dateStr2 = [format stringFromDate:date2];
NSLog(#"%#", dateStr2);
//#"dd/MM/YYYY HH:mm:ss"
//dd is for the day.
//MM is for the month.
//YYYY is for the year.
//HH is for the hour.
//mm is for the minute.
//ss is for the second.

Convert date from weird string to date in format day month and year

Hi I am parsing an end point of API, from there I am getting a string which contain like this date string 2016-01-18T13:28:06.357 but I want to convert this like 01 Jan 16, I have tried many times by using different format one of following mentioned. kindly need suggestion how to accomplish this task.
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSDate *myDate = [dateformat date];
// Objective C
NSString * date = [dictionary objectForKey:#"date"];
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[dateformat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *myDate = [dateformat dateFromString:date];
[dateformat setDateFormat:#"dd MMM yyyy"];
NSString *strDate=[dateformat stringFromDate:myDate];
// Swift 3.0
var date = (dictionary["date"] as! String)
var dateformat = DateFormatter()
dateformat.timeZone = NSTimeZone(name: "EST")
dateformat.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
var myDate = dateformat.date(fromString: date)!
dateformat.dateFormat = "dd MMM yyyy"
var strDate = dateformat.string(from: myDate)
You should use this format:
NSString *dateStr = #"2016-01-18T13:28:06.357";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [formatter dateFromString:dateStr];
// date is 2016-01-18 08:28:06 +0000
And next convert that date to format you need
You can also use this :
NSString *dateString = #"2016-01-18T13:28:06.357";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [dateFormatter dateFromString:dateString];
// Convert date object into desired format
[dateFormatter setDateFormat:#"dd MMMM yy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(#"newDateString=%#",newDateString);
You should use this format code
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Get Date
[formatter setDateFormat:#"dd"];
NSString *strDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"Date is - %#",strDate);
//Get Month
[formatter setDateFormat:#"MMM"];
NSString *strMonth = [formatter stringFromDate:[NSDate date]];
NSLog(#"Month is - %#",strMonth);
//Get Year
[formatter setDateFormat:#"yyyy"];
NSString *strYear = [formatter stringFromDate:[NSDate date]];
NSLog(#"Year is - %#",strYear);
Log is :
Date is - 05
Month is - Feb
Year is - 2016
for set more format, Click Me

Converting string to date with NSDateFormatter

Im trying to convert this string '2015-05-08T09:44:25.343' format to date 'dd/MM/YYYY' format
What I`m doing wrong? my date comes Nil.
// convert 2015-05-08T09:44:25.343 to dd/MM/yyyy
NSString *str = #"2015-05-08T09:44:25.343";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormat dateFromString:str];
NSLog(#"%#",date);
NSString *convertedString = [dateFormat stringFromDate:date];
NSLog(#"%#", convertedString);
NSString *str = #"2015-05-08T09:44:25.343";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];// this string must match given string #"2015-05-08T09:44:25.343"
NSDate *date = [dateFormat dateFromString:str];
NSLog(#"%#",date);
[dateFormat setDateFormat:#"MM/dd/yyyy"];// this match the one you want to be
NSString *convertedString = [dateFormat stringFromDate:date];
NSLog(#"%#", convertedString);
Your format string does not match up with the format of the date you provided. Try this.
NSString *str = #"2015-05-08T09:44:25.343";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormat dateFromString:str];
Then you can convert as needed.

how to convert nsdate to another format using nsdateformatter

I have date string in this format Feb 23, 2014 coming from web services, I want to convert it to this format 02-23-2014, I am trying a lot but all the time nsstring return me null value
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateStyle:NSDateFormatterMediumStyle];
[DateFormatter setDateFormat:#"MM-dd-yyyy"];
// date formatter two
NSDate *dateVal = (NSDate *)bookDetail.classdate;
NSLog(#"date %#",dateVal);
NSString *dateTwo = [DateFormatter stringFromDate:dateVal];
NSLog(#"new date string %#",dateTwo);
NSString *dateString = #"Feb 23, 2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd, yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];

Resources