C# WP8工具包日期选择器返回值格式

C# WP8工具包日期选择器返回值格式,c#,windows-phone-8,datepicker,wptoolkit,C#,Windows Phone 8,Datepicker,Wptoolkit,是否有任何方法可以将日期选择器返回的值更改为yyyy-MM-dd格式?我将值插入到SQLite数据库中,因此它必须采用该格式才能对值执行datetime语句 DateTime formatStartDate = DateTime.Parse(StartDate); StartDate= formatStartDate.ToString("yyyy-dd-MM"); DateTime formatEndDate = DateTime.Parse(EndDate); EndDate = f

是否有任何方法可以将日期选择器返回的值更改为yyyy-MM-dd格式?我将值插入到SQLite数据库中,因此它必须采用该格式才能对值执行datetime语句

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

 DateTime formatEndDate = DateTime.Parse(EndDate);
 EndDate = formatEndDate.ToString("yyyy-dd-MM");
这就是我一直在做的,而且在某种程度上是有效的。当我将字符串设置为yyyy-MM-dd时,它会将其全部更改,并将其作为yyy-dd-MM插入数据库中,因此我必须将字符串更改为yyy-dd-MM来解决这个问题,并将其以yyy-MM-dd格式插入数据库中

我可以使用它,因为它做了我希望它做的事情,即使它没有意义,但是当我尝试验证StartDate和EndDate时,无法选择之前的日期,它一定会弄糊涂,因为我的代码中有太多不同的日期格式,它试图将上面的格式化日期与DatePicker附带的默认日期值进行比较

我如何验证

 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.");
看起来这是一个很简单的解决办法,但我真的无法在网上找到解决办法。我读过的大多数帖子或文章似乎都只涉及前端格式。是否有任何方法可以将日期选择器返回的值更改为yyyy MM dd

StartDate和EndDate是绑定到日期选择器的字符串

编辑

在我的“保存”按钮中,我希望格式为yyyy-MM-dd,但插入时格式会更改为yyyy-dd-MM,因此我将格式设置为yyyy-dd-MM,并按照我实际希望的方式插入

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

为什么不将StartDate和EndDate改为DateTime属性呢?如果这不是一个选项,那么您可以在绑定中指定一个值转换器。每当我将StartDate和EndDate更改为DateTime属性时,它都会导致应用程序崩溃,日期为01/01/0001或类似的内容。转换器如何工作?只是尝试使用值转换器,但它不工作,并继续将日期设置为1/1/0001。我将DateTime属性更改为DateTime?,但这只会导致一大堆其他问题。你有什么XAML我们可以看一下吗?是的,我会编辑打开我的文章,以便更容易阅读。
                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");
        }
    }