0

Is there any way where you can change the returned value from a DatePicker to be in the format of "yyyy-MM-dd"? I'm inserting the value into a SQLite database so it has to be in that format to be able to perform datetime() statements on the values.

 DateTime formatStartDate = DateTime.Parse(StartDate);
 StartDate= formatStartDate.ToString("yyyy-dd-MM");

 DateTime formatEndDate = DateTime.Parse(EndDate);
 EndDate = formatEndDate.ToString("yyyy-dd-MM");

That's what I've been going with and it works to some extent. When I have the string as "yyyy-MM-dd" it changes it all around and inserts into the database as "yyyy-dd-MM", so I've had to change the string to "yyyy-dd-MM" to get around that problem and it inserts how I want it to in the database as "yyyy-MM-dd" format (weird).

I could work with this as it does what I want it to do even if it makes no sense, but when I try to validate the StartDate and EndDate so that dates before "now" can't be chosen, it must get all confused because there's so many different date formats going around in my code and it's trying to compare the formatted dates from above with the default date value that comes with the DatePicker.

How I'm validating;

 if (DateTime.Parse(StartDate) < DateTime.Now)
    MessageBox.Show("Please select a start date that is today or in the future.");
 if (DateTime.Parse(EndDate) < DateTime.Now)
     MessageBox.Show("Please select an end date that is today or in the future.");

Seems like such a simple fix, but I can't really find any way around it online. Most posts or articles I read seem to just deal with the front-end format. Is there any way that I can change the value returned from the DatePicker as "yyyy-MM-dd"?

(StartDate and EndDate are strings bound to DatePickers)

EDIT;

                <tk:DatePicker 
                    Grid.Row="1"
                    Grid.Column="0"
                    FontSize="20"
                    Value="{Binding StartDate, Mode=TwoWay}"
                    Background="LightGray" 
                    Foreground="#b71918"
                    BorderThickness="0"
                    Width="200"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"/>

                <tk:DatePicker 
                    Grid.Row="1"
                    Grid.Column="1"
                    FontSize="20"
                    Value="{Binding EndDate, Mode=TwoWay}"                 
                    Background="LightGray" 
                    Foreground="#b71918"
                    BorderThickness="0"
                    Width="200"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"/>

In my save button (I want the format as "yyyy-MM-dd" but it changes to "yyyy-dd-MM" when it's inserted, so I formatted as "yyyy-dd-MM" and it inserts as "yyyy-MM-dd", the way I actually want it to);

                DateTime formatStartDate = DateTime.Parse(StartDate);
                string fsd = formatStartDate.ToString("yyyy-dd-MM");

                DateTime formatEndDate = DateTime.Parse(EndDate);
                string fed = formatEndDate.ToString("yyyy-dd-MM");

                // gets all confused cause of the above formatting
                if (DateTime.Parse(StartDate) < DateTime.Now)
                    MessageBox.Show("Please select a start date that is today or in the future.");

                if (DateTime.Parse(EndDate) < DateTime.Now)
                    MessageBox.Show("Please select an end date that is today or in the future.");

                var insertGoal = new GoalsTrackerModel
                {
                    GoalName = GoalName,
                    GoalDesc = GoalDesc,
                    StartDate = fsd,
                    EndDate = fed,
                    IsOpen = 1,
                    IsComplete = 0
                };
                _dbHelper.Insert<GoalsTrackerModel>(insertGoal);
                MessageBox.Show("New goal saved. Good luck!");

    public string StartDate
    {
        get { return _startDate; }
        set
        {
            if (_startDate != value)
                _startDate = value;

            OnPropertyChanged("StartDate");
        }
    }

    public string EndDate
    {
        get { return _endDate; }
        set
        {
            if (_endDate != value)
                _endDate = value;

            OnPropertyChanged("EndDate");
        }
    }
FUBAR
  • 89
  • 2
  • 10
  • Why not change the StartDate and EndDate to DateTime properties instead? If that's not an option then you can have a value converter specified in the binding. – Adil Z Apr 10 '15 at 12:39
  • Whenever I change StartDate and EndDate to DateTime properties it causes the app to crash and has the date as 01/01/0001 or something similar. How would the converter work? – FUBAR Apr 10 '15 at 12:43
  • Just tried using a value converter but it didn't work and continued to set the date to 1/1/0001. I changed DateTime property to DateTime?, but that just caused a whole load of other problems. – FUBAR Apr 10 '15 at 13:18
  • Have you got any XAML we could have a look at? – Adil Z Apr 10 '15 at 13:22
  • Yeah, I'll edit opening my post so it's easier to read. – FUBAR Apr 10 '15 at 13:49
  • When you did turn it into a DateTime, did it throw any errors? – Adil Z Apr 10 '15 at 14:54

0 Answers0