I need to post a NSDate object using HTTP post to parse.com.
I don't know how to convert it.
//1 nsdate
[params setObject:date forKey:kKeyDate];
//2 nsstring
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd T HH : mm : ss.SSSZ"];
NSString *newString = [dateFormat stringFromDate:date];
[params setObject:newString forKey:kKeyDate];
Params are serialized in the body of the Http request. I know that everything else I'm doing is correct because if I comment out adding the NSDate to the params, then it works.
try this ..
NSDate *CurrentDate = [NSDate date];
NSDateFormatter *DATEFORMATER = [[NSDateFormatter alloc]init];
[DATEFORMATER setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];
[DATEFORMATER setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *DATE_STRING = [DATEFORMATER stringFromDate:CurrentDate];
If you are using iOS 10.0 or above, use the ISO8601DateFormatter class.
According to the Parse.com documentation:
{
"__type": "Date",
"iso": "2011-08-21T18:02:52.249Z"
}
Obj-C
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSString *newString = [dateFormat stringFromDate:date];
[params setObject:newString forKey:kKeyDate];
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"];
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;
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.
I tried to format a String from NSUserDefaults, which holds a Date.
The result is (null).
My question is: Is there a way to convert this String into a date to format it, or is there a better way?
My Code:
NSString *c = [prefs objectForKey:#"expiration_date_full"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:c];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# %# Noch gültig bis: %#", a, b, dateFromString];
You can store an NSDate in NSUserDefaults without conversion. That is, NSDate is one of the types supported by PLISTs. You can check the documentation for more info.
change your date format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate* myTime = [dateFormat dateFromString:#"07-22-2014"];
NSLog(#"%#",myTime);
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.