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.
Related
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"];
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);
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.
I have date string in the format 2016-04-27T08:06:07.531Z and want to convert into 2016.04.27. I have tried following code
NSString *dateString = #"2016-04-27T08:06:07.531Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
NSDate *dateReceived = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"dd.MM.YYYY"];
NSString *dateReq = [dateFormatter stringFromDate:dateReceived];
NSLog(#"Date Req:%#",dateReq);
I am getting null in console for NSLog. Any help will be appreciated. Thanks.
use like
NSString *dateString = #"2016-04-27T08:06:07.531Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dateReceived = [dateFormatter dateFromString:dateString];
// 2016.04.27
[dateFormatter setDateFormat:#"yyyy.MM.dd"];
NSString *dateReq = [dateFormatter stringFromDate:dateReceived];
NSLog(#"Date Req:%#",dateReq);
Plz just change your date formatter's date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
instead of
[dateFormatter setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
try this it will help you --
NSString *date = #"2011-02-27T08:06:07.531Z";
NSDateFormatter *dFormat = [[NSDateFormatter alloc] init];
[dFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *dReceive = [dFormat dateFromString:dateString];
[dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
NSString *dateReq = [dateFormatter stringFromDate:dReceive];
just change date format
[dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
Use date format as: [dFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.Z"];
Hi in my project i get the date format in json i have tried many formats.it didnt worked out.ExpiryDate is my keyvalue stored in dictionary.please let me know the conversion for this.I need a format like this 07/Jan/2016.
cell.Expire.text=[ResDiction objectForKey:#"ExpiryDate"];
NSString *dateString = [ResDiction objectForKey:#"ExpiryDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];
My json result:
CreateUserId = 2;
ExpiryDate = "2016-01-07T00:00:00";
Try this
cell.Expire.text=[NSString stringWithFormat :"%#",[ResDiction objectForKey:#"ExpiryDate"]];
Your conversion from sting to date seems to be correct.
However, you need to use another NSDateFormatter in order to output the date in the other format.
NSString *dateString = [ResDiction objectForKey:#"ExpiryDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"]; //Will convert from string to native date object
NSDate *date = [dateFormatter dateFromString:dateString];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd/MMM/yyyy"]; //will convert into string of type 07/Jan/2016
cell.Expire.text = [dateFormatter2 stringFromDate:date];
Hi i juz found the answer.Thanks for all your response
NSString * dateStr = [ResDiction objectForKey:#"ExpiryDate"];
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%#",datePart];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",newDateStr);
cell.Expire.text=newDateStr;