Date format does not work properly - iphone

I take date into string. date format is like this and string is
datenotification=20-02-2012
Now I want to change this into
20-feb-2012
but for checking purposes I just place that into a dateformatter and print that but it shows me an incorrect date
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-YYYY"];
NSDate *d=[currentdateformate dateFromString:dateNotification];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];

Hey Check this example
NSString *string = #"12-02-2000";
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-yyyy"];
NSDate *d=[currentdateformate dateFromString:string];
[currentdateformate setDateFormat:#"dd-MMM-yyyy"];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
NSLog(#"S %#",str3);
output S 12-feb-2012

Related

iPhone: How to convert "yyyyMMddThhmmss" format string to NSDate?

I have a string like "20121124T103000" and I want to convert this to NSDate.
I tried converting it with dateFormatter date format "yyyyMMddThhmmss" but it gives output as 2001-01-01 00:00:00 +0000 which is incorrect.
What is the way we can convert this string to NSDate?
Here is my code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMddThhmmss"];
NSLog(#"%#",[dateFormat dateFromString:#"20121124T103000"]);
Any help is appreciated.
Your original code is quite close; instead of:
#"yyyyMMddThhmmss"
You should be using:
#"yyyyMMdd'T'hhmmss"
The only difference is the pair of single quotes around the 'T' in the string- you just need to let the app know that 'T' is a part of the formatting, not the actual date.
do like this,
NSString *dateStr = #"20121124T103000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd'T'hhmmss"];
NSDate *d = [dateFormat dateFromString:dateStr];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st);
Try the following Code.
NSString *dateStr = #"20121124T103000";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"yyyyMMdd'T'hhmmss"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd'T'hhmmss"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st]);

How to specify the conversion format of a date to a string

I am creating an app where the current date needs to be displayed in a label and automatically updated each day. I have looked at a few tutorials and they have all shown how to get the date in YYYY:DD:MM hh:mm, I am wondering how to change this format to DD/MMM/YYYY.
Any sample code or links to tutorials would be greatly appreciated.
Something like this:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateformatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
[myLabel setText:[formatter stringFromDate:today]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *dateString = [dateFormatter stringFromDate: dayDate];
//display dateString in label
[dateFormatter release];
You need to use the NSDateFormatter. For specfic formats look at
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];

String to a Format yyyy-MM-dd HH:mm:ss Iphone

I have an nsstring ,see below
NSString *Mydate=#"9/8/2011"; in month/day/year format.
I want this string to be in the format yyyy-MM-dd HH:mm:ss.
for eg: 2011-09-08 15:51:57
so that i need to show the string in a label in the later format.
Thanks all.
Try below code with valid input string that will help you.
NSString *dateStr = #"9/8/2011 11:10:9";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"d/M/yyyy hh:mm:s"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:s"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st);
[dtF release];
[dateFormat release];
You may use NSDateFormatter

unable to fetch NSDate from NSString

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

Date Conversion in objective c?

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