Json date conversion in uilabel with keyValue - ios

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;

Related

Convert date to string with different date format 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);

Convert date string(2016-04-27T08:06:07.531Z) to specific date format dd.MM.YYYY

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

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 date string in 2014-05-28T10:51:40.3056+01:00 to dd-mmm-yyyy format in ios

I'm very new to this ios. I want to convert 2014-05-28T10:51:40.3056+01:00 to 28-may-2014.But I'm getting null. please help me .
//here dateString is 2014-05-28T10:51:40.3056+01:00
NSString * dateString= [dict objectForKey:#"created_date"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[df setDateFormat: #"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *inputDate = [df dateFromString: dateString];
[df setDateFormat:#"dd-MMM-yy"];
NSString * inputValue = [df stringFromDate:inputDate];
Try this :
NSString * dateString = #"2014-05-28T10:51:40.3056+01:00";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
[df setDateFormat: #"yyyy-MM-dd'T'hh:mm:ss.SSSZ"];
NSDate *inputDate = [df dateFromString: dateString];
[df setDateFormat:#"dd-MMM-yy"]; // [df setDateFormat:#"dd-MMM-yyyy"];
NSString *inputValue = [df stringFromDate:inputDate];
Output: 28-May-14
Well, I have been facing the same issue but then I split the Date string from . because it's not take the formate
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
if ([dateFormat dateFromString:[[value componentsSeparatedByString: #"-"] objectAtIndex:0]] == nil) {
value = [dateFormat dateFromString:[[value componentsSeparatedByString: #"-"] objectAtIndex:0]];
} else {
value = [dateFormat dateFromString:[[value componentsSeparatedByString: #"."] objectAtIndex:0]];
}

NSDateFormatter & NSDate dateFromString outputs null

NSString *dateString = [[items objectAtIndex:indexPath.row] objectForKey:#"date"];
NSLog(#"DATE: %#",dateString);
// outputs 16/10 23:59:49
NSDateFormatter *dateFormat;
[dateFormat setDateFormat:#"dd/MM HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"DATE: %#", date);
// outputs null
but it outputs null every time.
please can you help me what am i doing wrong?
You forgot to create NSDateFormatter:
You should replace line:
NSDateFormatter *dateFormat;
With:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
You need create
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
You have to set year otherwise date will be shown null. You can use current year and separated your string into months and days then set like this "dd-MM-yyyy" or any another way which you want.
NSString *string = #"16/10 23:59:49";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSArray *arrayDateData = [string componentsSeparatedByString:#"/"];
NSString *dayStr =[arrayDateData objectAtIndex:0];
NSString *string1 =[arrayDateData objectAtIndex:1];
NSString *monStr = [[string1 componentsSeparatedByString:#" "] objectAtIndex:0];
NSString *timeString;
timeString=[NSString stringWithFormat:#"%d-%#-%#",2012,monStr,dayStr];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString=[df dateFromString:timeString];
NSLog(#"date from string: %#",dateFromString);
I think it will be helpful to you.

Resources