NSDate specific format - ios

I try to set a specific format to my NSString date, but I am a little confused about how to retrieve some part of my string.
My string date is : #"2008-06-26T00:00:00+0100" and I would like to transform it in "06/2008".
This is my method :
- (NSString *)formatDate: (NSString *) myDate {
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"MM/yyyy"];
NSDate * aDate = [df dateFromString:myDate];
NSString *stringFromDate = [df stringFromDate:aDate];
return stringFromDate;
}
I know that the format I try to set doesn't respect the format I put in setDateFormat but I don't know what to define, because I want to retrieve only the month and the year.

Try this :
(NSString *)formatDate: (NSString *) myDate {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:myDate];
NSDateFormatter *dF = [[NSDateFormatter alloc] init];
[dF setDateFormat:#"MM/yyyy"];
NSString *stringFromDate = [dF stringFromDate:dte];
return stringFromDate
}

Related

Unable to convert NSString to NSdate format

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

How to format date using NSDateFormatter iOS?

I tried to use the NSDateFormatter to format a date that selected from a UIDatepicker. But the formatted result giving a nil.
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MMM-dd HH:mm:ss";
NSDate *formattedDate = [dateFormatter dateFromString:date];
return [dateFormatter stringFromDate:formattedDate];
}
date input is 2016-04-22 16:30:36 +0000(after selected from UIDatePicker). How may I fix this?
As mentioned in the comment you can get the NSDate directly from the UIDatePicker and pass that date directly in the formatter
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:datePicker.date];//pass the date you get from UIDatePicker
If you want to convert the same way you have mentioned in the question you can try like this:
-(NSString *)formatDateWithString:(NSString *)date{
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate * convrtedDate = [formatter dateFromString:date];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:convrtedDate];
return dateString;
}

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;

How to convert NSString to NSDate in IOS?

I am building an application where I need to convert The NSString to NSDate. I have googled and found a lot links of SOF mentioned below -
https://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-
back-again
Convert NSString to NSDate
Converting can NSString to NSDate format in objective C
I had used the answer given there but the problem I still facing is explain below step by step.
I had taken NSString *StringDate = #"01:00:00"; // I have declared time in the NSString variable.
I used the conversion method that mentioned in above link and stored the converted value to the NSDate variable.
When I am NSLog the NSDate variable. I am get "19:30:00" in my logcat. While the expected result should be "01:00:00".
Here is one of the code I am using
NSString *dateString = #"01:00:00";
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:#"hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
-(NSString *)geTimeFromString:(NSString *)string
{
NSString * dateString = [NSString stringWithFormat: #"%#",string];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm:ss"];
NSDate* myDate = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"hh:mm a"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *stringFromTime = [dateFormatter stringFromDate:myDate];
return stringFromTime;
}
Try this
This is likely the correct date, but printed in a different timezone. I posted a longer explanation here:
Unexpected value from NSDate
BTW: In
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[[NSDate alloc] init] is completely meaningless, because you assign an instance of NSDate in the very next line.
Try this method to convert NSString to NSDate :-
- (NSDate*) convertStringToDate
{
NSString* dateStr = #"20/05/2015";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
return date;
}

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