Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:26:51+00:00 2026-06-01T07:26:51+00:00

I have a UIDatePicker and I get the date using this: NSDate *pickerDate =

  • 0

I have a UIDatePicker and I get the date using this:

   NSDate *pickerDate = [datePickerView date];

I want to create a date in the future which is based on the day of the week and the time (HH:mm:ss) from the pickerDate. Here is the code that I am using but the date that it generates is wrong:

UPDATED CODE

        //format the uidatepicker
        NSDate *dateSet = pickerDate;
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setTimeZone:[NSTimeZone localTimeZone]];
        [df setDateFormat:@"EEEE HH:mm:ss"];
        NSString *dateSetString = [df stringFromDate:dateSet];

        NSLog(@"repeatDateString %@",dateSetString);//prints "Tuesday 12:12:43"

        //create a new nsdate using the new format
        NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
        [dateFormat2 setTimeZone:[NSTimeZone localTimeZone]];
        [dateFormat2 setDateFormat: @"EEEE HH:mm:ss"];

        NSDate *newDate = [[NSDate alloc] init];
        newDate = [dateFormat2 dateFromString:dateSetString];

        NSLog(@"new Date %@",newDate);//prints "1970-01-06 04:42:43 +0000"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:26:52+00:00Added an answer on June 1, 2026 at 7:26 am

    The problem is that your date string does not contain all of the information from the picker, and does not contain enough information in order to re-create the date. It is missing the Day, Month, and Year so when you re-create it it assumes that you want to start at the start of iOS’s NSDate calendar system.

    You either need to store the NSDate that you receive from the picker (preferably), or you need to use a date format that contains enough information to create a specific date and time.

    EDIT
    Based on your comment, you want to just use the weekday and time from the datePicker and determine the next date in the future with those values. Here is code that will accomplish that:

    NSCalendar          *cal            = [NSCalendar currentCalendar];
    
    NSDate              *pickerDate     = self.datePickerView.date;
    NSDateComponents    *pickerComps    = [cal components:  NSWeekdayCalendarUnit |
                                                            NSHourCalendarUnit |
                                                            NSMinuteCalendarUnit |
                                                            NSSecondCalendarUnit
                                                 fromDate:pickerDate];
    
    NSDate              *currentDate    = [NSDate date];
    NSDateComponents    *currentComps   = [cal components:  NSYearCalendarUnit | 
                                                            NSMonthCalendarUnit | 
                                                            NSWeekdayCalendarUnit |
                                                            NSDayCalendarUnit |
                                                            NSHourCalendarUnit |
                                                            NSMinuteCalendarUnit |
                                                            NSSecondCalendarUnit
                                                 fromDate:currentDate];
    
    // Start with the current date and add/subtract the number of days needed to make it the same day of the week
    NSDateComponents    *newComps       = [currentComps copy];
    NSUInteger          weekdayDiff     = pickerComps.weekday - currentComps.weekday;
    newComps.day                        = newComps.day + weekdayDiff;
    // If needed, add 7 days in order to move this date into the future
    if (newComps.day < currentComps.day) {
        newComps.day = newComps.day + 7;
    }
    
    NSDate *newDate = [cal dateFromComponents:newComps];
    if ([newDate compare:currentDate] == NSOrderedAscending) {
        // This is true when the weekday started out the same but the time of day is earlier in the picker.  
        // Add 7 days
        newComps.day = newComps.day + 7;
        newDate = [cal dateFromComponents:newComps];
    }
    
    NSLog(@"%@", newDate);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSDate that I get from a UIDatepicker. IBOutlet UIDatePicker *dueDate; NSDate
Im trying to create a simple callback using blocks. I have a MainViewController which
I have a UIDatePicker that well, allows you to select a date (not time).
I have a date field (i'm using the jquery ui datepicker) in a form
I have used UIDatePicker in my app. I want to (actually have to) change
How do I get my date to display like this: yy-mm-dd inside my date
I have a UI which contains a UITableView and a UIDatePicker. When I select
I have just started using UIDatePicker in my iPad app, but is there a
I have in my Picker class: - (id)initWithFrame:(CGRect)frame withSender:(id)sender withDate:(NSDate*)date { if ((self =
I have a UIDatePicker that shows (ex. April | 13 | 2010). I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.