-1

i am new to iPhone application development. i am creating simple application with using of datepicker. i have a date picker and a button. simply i want to print selected date and time when i click on button. bellow is my code.

- (IBAction)showSelectedDate:(id)sender {    
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDate *pickerDate = [m_datePicker date];

    NSLog(@"%@", pickerDate); 
}

In log date is print but it is wrong. i am select "2012-03-28 03:26 PM" this date from picker and in log i get the date "2012-03-28 09:50".

Thanks in adv....

Bhavin_m
  • 2,746
  • 3
  • 30
  • 49
  • [m_datePicker date]; This 'm_datepicker' belongs to which class??, coz the 'date' it returns is being displayed in your log.! – hp iOS Coder Mar 28 '12 at 10:16
  • 2
    The apple document says that "The default is the date when the UIDatePicker object is created." So I think you are getting the default date. – hp iOS Coder Mar 28 '12 at 10:25
  • check out the answer here [http://stackoverflow.com/questions/4272953/uidatepicker-strange-behaviour-not-setting-date-and-wrong-date-being-selected][1] [1]: http://stackoverflow.com/questions/4272953/uidatepicker-strange-behaviour-not-setting-date-and-wrong-date-being-selected – Hiren Mar 28 '12 at 10:43

2 Answers2

1

Try this code... It may work fine for you.

- (IBAction)showSelectedDate:(id)sender
{
        NSDate *selectedDate = myPicker.date;

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"DD-MM-YYYY"];


    NSString *str = [formatter stringFromDate:selectedDate];
    NSLog(@" %@",selectedDate);
}

If u want time replace

[formatter setDateFormat:@"DD-MM-YYYY"];

with this line

[formatter setDateFormat:@"day, MMM HH:mm:ss Z"];

Hope this helps you.

Kiran
  • 339
  • 1
  • 14
  • 1
    this code print only date. but i don't have problem with date. i am having a problem with time. see my original post on upside. – Bhavin_m Mar 28 '12 at 10:30
  • [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; This is work for me...but when i going to convert string to date it's show wrong again. – Bhavin_m Mar 28 '12 at 10:59
  • please post your code.So that i can check where you making mistake. – Kiran Mar 28 '12 at 11:13
  • - (IBAction)showSelectedDate:(id)sender { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDate *pickerDate = [m_datePicker date]; NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init]; [formatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; NSString *str = [formatter2 stringFromDate:pickerDate]; NSLog(@"%@", str); – Bhavin_m Mar 28 '12 at 11:19
  • NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init]; [formatter3 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; [formatter3 setTimeZone:[NSTimeZone localTimeZone]]; NSDate *finalDate = [formatter3 dateFromString:str]; NSLog(@"%@",finalDate); } – Bhavin_m Mar 28 '12 at 11:20
  • Man learn how to Add comment and how to add code.Check stak overflow guidelines. – Kiran Mar 28 '12 at 11:30
  • `NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];` `[formatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];` `NSDate *str = (NSDate *)[formatter2 stringFromDate:pickerDate];` `NSLog(@"%@", str);` `NSDateFormatter *formatter3 = [[NSDateFormatter alloc] init];` `[formatter3 setTimeZone:[NSTimeZone localTimeZone]];` `[formatter3 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];` `NSDate *finalDate = [formatter3 dateFromString:str];` `NSLog(@"%@",finalDate);` – Bhavin_m Mar 28 '12 at 11:39
0

You are using GMT rather than local time so that it just looks wrong. Use

datePicker.timeZone = [NSTimeZone localTimeZone];
Maulik
  • 19,348
  • 14
  • 82
  • 137