在我的WPF项目中,我有一个DatePicker,但是如果我输出它,它会给我2018年11月2日。
但我需要它2018-10-2,因为我想要它的SQL。
我试过这段代码:
dpStart.Format = DateTimePickerFormat.Custom
DateTimePicker1.CustomFormat = "dd-MM-yyyy"但是说没有日期选择的格式。
我做错了什么?是否有一种将DatePicker格式转换为SQL的简单方法?
发布于 2018-11-02 13:06:17
但我需要它2018-10-2,因为我想要它的SQL。
由DateTime控件存储的DatePicker值没有特定的格式。但是,在将值转换为string时,您可以应用一种格式:
string formattedDate = dpStart.SelectedDate.Value.ToString("yyyy-MM-dd");但是,您确实应该考虑将实际的DateTime值传递到数据访问层,而不是将格式化的string传递到数据访问层。
发布于 2018-11-02 12:17:32
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate, StringFormat='dd-MM-yyyy',
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>使用上述代码将DatePicker控件日期的显示格式更改为“dd-MM”。
发布于 2018-11-02 10:30:09
你可以这样做
DateTime dpStartDate = (DateTime)dpStart.SelectedDate;
string result = dpStartDate.ToString("yyyy-MM-dd");https://stackoverflow.com/questions/53116762
复制相似问题