NSDateFormatter & NSDate dateFromString outputs null - ios

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.

Related

how to convert string date to nsdate using formatter?

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);
}

Json date conversion in uilabel with keyValue

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;

Convert/Format DateString from NSUserdefaults

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);

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 not working in TextField

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

Resources