i follow stackOverflow answer and Write a code but i Don't know What happen.it won't work. plz,any one can solve it for me. i use fooling code....
NSMutableArray *movieDate;
NSArray *temp1 = [_dicUpcomingMovie objectForKey:#"results"];
for (NSDictionary *temp2 in temp1)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSString *datestring = [temp2 valueForKey:#"release_date"];
//NSDate *myDate = [[NSDate alloc]init];
[formatter setDateFormat:#"dd MMM,YYYY"];
NSDate *date = [formatter dateFromString:datestring];
NSLog(#"%#",date);
[movieDate addObject:date];
NSLog(#"%#",movieDate);
}
so this is it. the above code was not work myDate object still nil
I believe that datestring doesn't match the format you specified. See NSDateFormatter for a complete example.
NSMutableArray *movieDate=[[NSMutableArray alloc]init];;
NSArray *temp1 = [_dicUpcomingMovie objectForKey:#"results"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
for (int i=0 ;i<temp1.count;i++)
{
NSString *datestring = [[temp1 objectatindex:i]valueForKey:#"release_date"];
[serverFormatter setTimeZone:[NSTimeZone localTimeZone]];
[serverFormatter setDateFormat:#"dd,MMM,YYYY"];
NSDate *date = [serverFormatter dateFromString:dateString1];
[movieDate addObject:date];
}
NSLog(#"%#",movieDate);
Try this,
NSMutableArray *movieDate;
NSArray *temp1 = [_dicUpcomingMovie objectForKey:#"results"];
for (NSDictionary *temp2 in temp1)
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSString *datestring = [temp2 valueForKey:#"release_date"];
//NSDate *myDate = [[NSDate alloc]init];
[formatter setDateFormat:#"dd MM,YYYY"];
NSDate *date = [formatter dateFromString:datestring];
NSLog(#"%#",date);
[movieDate addObject:date];
NSLog(#"%#",movieDate);
}
Related
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;
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];
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]];
}
OK, I know I am missing something but I don't know what.
My NSDateFormmater comes up in the textField(personMonthdayTextField) with todays date but when I selected the datePicker and input it into the textField, it comes up with the standard format with the hours, minutes, seconds which I don't need.
Any help is appreciated.
Thanks
-(void)viewDidLoad {
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
NSString* str = [formatter stringFromDate:date];
NSLog(#"%#",str);
NSMutableArray *Arr=[str componentsSeparatedByString:#" "];
personMonthdayTextField.text=[Arr objectAtIndex:0];
}
- (IBAction)done:(id)sender {
NSDate *dateSelected = [datepick date];
NSString *dateStamp = [[NSString alloc]initWithFormat:#"%#", dateSelected];
personMonthdayTextField.text = dateStamp;
}
This will give you date only
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:#"%#",
[df stringFromDate:[NSDate date]]];
Adapt this for your code under your IBAction method
just use NSDateFormatter in your IBAction
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *dateSelected = [datepick date];
personMonthdayTextField.text = [dateFormatter stringFromDate:dateSelected];
you can also tweak with formats of date give in Apple ios sdk ,easily to get desired format.
If you only want the day why don't you set the dateFormat as "dd", it would save the need to separate the components of dateString.
-(void)viewDidLoad {
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd"];
personMonthdayTextField.text = [formatter stringFromDate:date];
}
- (IBAction)done:(id)sender {
NSDate *dateSelected = [datepick date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd"];
personMonthdayTextField.text = [formatter stringFromDate:date];
}
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.