I store some dates in my NSMutableArray, I want to retrieve them and change the format.
For the moment I do this:
NSDate *myDate = [self.dates objectAtIndex:0];
NSLog(#"myDate:%#", myDate); // 2010-03-02
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:#"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
NSLog(#"newDate: %#", newDate); // NULL
the result for newDate is NULL, and I want it to be something like that: "March 3, 2010"
thanks,
That works for me, but I replaced the first line with:
NSDate *myDate = [NSDate date];
My output:
2010-03-02 19:29:08.045 app[11464:a0f] myDate:2010-03-02 19:29:08 -0800
2010-03-02 19:29:08.048 app[11464:a0f] newDate: March 2 2010
My best guess is that [self.dates objectAtIndex:0] isn't returning what you want it to.
Since your dates array contains strings, you have to convert the string to an NSDate and then you can re-convert the date to the format you want.
NSDateFormatter *strToDateFmt = [[NSDateFormatter alloc] init];
[strToDateFmt setDateFormat:#"yyyy-MM-dd"];
NSDate *myDate = [strToDateFmt dateFromString:[self.dates objectAtIndex:0]];
[strToDateFmt release];
NSLog(#"myDate:%#", myDate);
NSDateFormatter *formatDate = [[NSDateFormatter alloc] init];
[formatDate setDateFormat:#"MMMM d YYYY"];
NSString *newDate = [formatDate stringFromDate:myDate];
[formatDate release];
NSLog(#"newDate: %#", newDate);
Related
Can someone tells me whats wrong with my time formatter? when i used ipod 6.0 the time formatter works. but when i used iphone 5 6.1 the time formatter returns nil.
Here are my codes regarding to the timeFormatter.
NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init] autorelease];
[tempFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *dateandtime = #"2013-05-27 19:00";
//set end date and time
NSDateFormatter *tempFormatterTo = [[[NSDateFormatter alloc]init] autorelease];
[tempFormatterTo setDateFormat:#"yyyy-MM-dd hh:mm"];
NSString *dateandtimeTo = #"2013-05-27 20:00";
NSDate *startDate = [tempFormatter dateFromString:dateandtime];
NSDate *endDate = [tempFormatterTo dateFromString:dateandtimeTo];
Thanks in advance. please help me.
Try to use this format. HH used for 24 hour format.
[tempFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
Try This
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm"];
NSDate* d = [df dateFromString:#"2013-05-27 19:00"];
NSLog(#"%#", d);
here problem in your Format change it to yyyy-MM-dd HH:mm also use my bellow method when you want to convert any NSString date to NSDate .. This is my custom method just paste this method in your .m class..
-(NSDate *)convertStringToDate:(NSString *) date dateFormat:(NSString*)dateFormat{
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:dateFormat];// set format here which format in string date
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
and use above method like bellow...
NSDate *startDate = [self convertStringToDate:#"2013-05-27 19:00" dateFormate:#"yyyy-MM-dd HH:mm"];
NSDate *endDate = [self convertStringToDate:#"2013-05-27 20:00" dateFormate:#"yyyy-MM-dd HH:mm"];
I have an NSDate object and must create a label that indicates the day of the week in a short 3-char style like 'Mon', 'Tue', 'Wed', ... and localized to the short representation in any language.
The date formatter EEE has 3 chars but how does that translate to languages like Chinese which probably only have one character?
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE"];
NSString *strDate = [formatter stringFromDate:date];
[formatter release];
//Commented out the following line as weekday can be of one letter (requirement changed)
//strDate = [strDate substringToIndex:3]; //This way Monday = Mon, Tuesday = Tue
To use the correct locale you'll want to set the dateFormat on the instance of NSDateFormatter using the + dateFormatFromTemplate:options:locale: method.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:#"EEE" options:0 locale:[NSLocale currentLocale]];
NSLog(#"Weekday using %#: %#", [[NSLocale currentLocale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Test what Chinese will look like without changing device locale in settings
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"zh_CN"];
NSLog(#"Weekday using zh_CN: %#", [dateFormatter stringFromDate:date]);
This prints:
Weekday using en_US: Thu
Weekday using zh_CN: 周四
This will print what you need:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
NSString *day = [formatter stringFromDate:date];
[formatter release];
if ([day length] > 3) day = [day substringToIndex:3];
NSLog(#"%#", day);
HI i need to increment 15 minutes in time intrval which i get in the webservice .
The code is as follows
NSString *dateString =obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate *scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter release];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *strTime = [dateFormatter1 stringFromDate:scheduledForYellow];
[dateFormatter1 release];
NSArray *aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
Here to add 15 minutes to my start time i have written the above code , but in end time that is [aryy objectAtIndex:1] does not give me the correct time.What i wanted to do is increment 15 minutes for whatever date i get in start date .Dont know whats the issue is.
`
It must be because of the default timezone (local timezone) that the date formatter uses to generate the string. I have modified your code a bit to reuse the date formatter and fixed a leak with dateFromString.
NSString *dateString = obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate * dateFromString = [dateFormatter dateFromString:dateString];
NSDate * scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString * strTime = [dateFormatter dateFromString:scheduledForYellow];
NSArray * aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
This should fix the issue. Let me know if you face any issues.
I am able to fetch NSString representing the current date and time in PST using the following code.
//get current date and time
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:#"America/Los_Angeles"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
[dateFormatter setTimeZone:timeZone];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];
NSDate *date = [NSDate date];
NSString *currentDateStr = [dateFormatter stringFromDate:date];
NSLog(#"currentDateStr %#",currentDateStr);
[dateFormatter release];
EDIT:
But the following code to fetch NSDate from NSString doesn't serve my purpose.
//get date from string
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSDate *dt = [[NSDate alloc]init];
dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
"dt" appears to be null.
Could anybody help me out in solving this issue of getting NSDate from NSString.
Thanx in advance.
NSDateFormatter also has dateFromString: function.
EDIT:
//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
Make use of
[dateFormatter dateFromString:urString];
In your case urString is currentDateStr
Use
- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
NSLog(#"I am inside the datePickerValueChanged - - %#", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}
I have an array containing the String objects with this format 22/04/2011.My question is that how can I change this string into the yyyy-mm-dd format. I am using NSDateFormatter but it's not working.
The real thing to know that when you convert a string date to NSDate using NSDateFormatter using any of the above mentioned methods, you will always get the NSDate object in Local TimeZone. So when coding the method for date conversion you need to consider this factor.
Hope this helps..
-:samfisher
Try this,
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:#"22/04/2011"];
[formater setDateFormat:#"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];
NSLog(#"Date - %#",result);
And For Reference - http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Here how I do this ... This code works ... Try this
NSDate* currentDate = [NSDate date];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-mm-dd"];
NSString* currentDateString = [df stringFromDate:currentDate];
[df release];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:#"dd/mm/yyyy"];
NSDate *date2 = [formater dateFromString:#"2010-11-17"];
[formater setDateFormat:#"yyyy-mm-dd"];
NSString *result = [formater stringFromDate:date2];