Getting month from UIPickerView - iphone

I have a unique requirement. In my app I need to get only the month from picker. Since Date picker cannot implement this, I made the custom picker listing only months. The required format of the date after the customer selects a month is 1-Jan-2011 to 1-feb-2011
Basically, the month is the only thing that is coming from uipickerview and the day and year are static.
Can someone help me out? Some code I have tried.
NSString *dateString = [[NSString alloc ]initWithFormat:#"2011-%d-1 GMT+05:30", monthFromPickerAsInteger];
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:#"yy-MM-DD z4"];
NSDate *dateFromString = [[NSDate alloc] init];
// ta-daaa!
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#", dateFromString);

I guess you want to do this,
NSString *dateString = [NSString stringWithFormat:#"2011-%d-1", monthFromPickerAsInteger];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-M-d"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"d-MMM-yyyy"];
NSString *requiredDateString = [dateFormatter stringFromDate:dateFromString];
[dateFormatter release];
NSLog(#"%#", requiredDateString);

Related

How to change the date format in iphone sdk

I'm getting Response from Webservices Date like "2012-08-17T00:00:00". I want to Display Date only like 17-08-2012. How Change Date format and Remove that time...
//String froms web service(ws)
NSString *stringFromWS =[NSString stringWithString:#"2012-08-17T00:00:00"];
//date formatter for the above string
NSDateFormatter *dateFormatterWS = [[NSDateFormatter alloc] init];
[dateFormatterWS setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date =[dateFormatterWS dateFromString:stringFromWS];
//date formatter that you want
NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:#"dd-MM-yyyy"];
NSString *stringForNewDate = [dateFormatterNew stringFromDate:date];
NSLog(#"Date %#",stringForNewDate);
also refer the link for more explanation
http://mobiledevelopertips.com/cocoa/date-formatters-examples-take-2.html
NSDate *today = [NSDate date];
NSLog(#"Date today: %#", today);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
dateFormat.dateFormat = #"dd-MM-yyyy";
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"Date: %#", dateString);
Also see the below date and time format styles
NSDateFormatterNoStyle
NSDateFormatterShortStyle
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
NSDateFormatterFullStyle
You can set it like [dateFormat setDateStyle:NSDateFormatterShortStyle];

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

Converting NSString to NSDate with date and time being received as different string

I know this have been asked a lot but I am still having the problem in this conversion even after extensive search on the net. Can some one point me to my mistake in following code?
NSString *eventDttm = [[NSString alloc] init];
eventDttm = [NSString stringWithFormat:#"%# %#",myEvent.startDate,myEvent.timeFrom];
NSLog(#"eventDttm: %#",eventDttm);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM a"];
//conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:eventDttm];
[formatter release];
NSLog(#"%#",dateFromString);
I am getting date and time separately in string as following:
myEvent.startDate= 2011-11-22
myEvent.timeFrom= 08:00 PM
but dateFromString is always null
Don't mix Months and minutes (MM & mm)
[formatter setDateFormat:#"yyyy-MM-dd HH:mm a"];

Accurate date formate and compare date in iphone dev?

I am new in iphone, i want to change string into accurate date-formate which i couldn't done this for last 2 hrs. I have string like(17:30 18 Oct) and want to convert this in accurate formate and also compare this date to tomorrow if this in tomorrow then display just 18 Oct and if it on today just display time 17:30, any help please.
NSString to NSDate conversion
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Try this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];

Accurate Date formate for hours time and date month in iphone? [duplicate]

I am new in iphone, i want to change string into accurate date-formate which i couldn't done this for last 2 hrs. I have string like(17:30 18 Oct) and want to convert this in accurate formate and also compare this date to tomorrow if this in tomorrow then display just 18 Oct and if it on today just display time 17:30, any help please.
NSString to NSDate conversion
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Try this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];