从 DatePicker 以 yyyy/MM/dd 格式获取日期

Posted

技术标签:

【中文标题】从 DatePicker 以 yyyy/MM/dd 格式获取日期【英文标题】:Getting date from DatePicker in format yyyy/MM/dd 【发布时间】:2012-12-09 18:12:55 【问题描述】:

我有一个 WPF C# 应用程序 (.NET 4.0) 和一个日期选择器。我想知道是否可以将格式日期(日期选择器的文本属性)更改为yyyy/MM/dd 格式。

我正在使用 MVVM 模式,所以我想知道 XAML 代码,如果可以在这个地方做的话。

【问题讨论】:

【参考方案1】:

我可以使用这个代码:

<DatePicker SelectedDate="Binding myVideModelProperty"
                    Height="25" HorizontalAlignment="Left" Margin="81,-2,0,0" Name="myDatePicker" VerticalAlignment="Top" Width="115">
            <DatePicker.Resources>
                <Style TargetType="x:Type DatePickerTextBox">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBox x:Name="PART_TextBox" 
                                    Text="Binding Path=SelectedDate, RelativeSource=RelativeSource AncestorType=x:Type DatePicker, StringFormat=0:yyyy/MM/dd" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DatePicker.Resources>
        </DatePicker>

使用 datePicker 资源,我可以设置其文本框的格式。这样,就不需要转换器,也不需要通用模板和任何其他额外的代码。

【讨论】:

【参考方案2】:

根据这个post,您可以更改文化以获得正确的日期格式

System.Globalization.CultureInfo culInfo = new System.Globalization.CultureInfo("en-us");
System.Globalization.DateTimeFormatInfo dtinfo = new System.Globalization.DateTimeFormatInfo();
dtinfo.ShortDatePattern = "yyyy/MM/dd";

【讨论】:

但这是在 axml 中?我想使用 MVVM 模式。谢谢。 这没有放在 xaml 中,请您在示例中插入 som 代码,以便我更清楚地理解问题。 @Daimroc MVVM 并不意味着“在 XAML 中做所有事情”。 @svick:但我认为它会在 axml 中进行演示。如果我有一个多语言应用程序,我只想更改日期的格式,我认为这是 axml 的责任。 Xaml 中无法指定格式。所以你的选择是:调整文化设置,或者重建控件以支持日期格式。 MS 没有考虑到所有事情,如果我们必须等到他们发布任何东西之前发生这种情况,我们现在可能已经很幸运拥有了 .Net 1.0 测试版。 :)【参考方案3】:

将此用于您的日期选择器的Template,并使用此值转换器。在模板中,带有DatePickerTextBox 的行已被修改为使用您请求的yyyy/MM/dd 格式(通过转换器)。我修改的模板来自here。

示例用法:

<DatePicker Template="StaticResource SpecialDatePicker" SelectedDate="Binding Path=Date" />

值转换器代码:

public class DateFixerConverter : IValueConverter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    
        return DateTime.Parse((string)value).ToString("yyyy/MM/dd");
    

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    
        return DateTime.Parse((string) value);
    

模板代码:

    <ControlTemplate x:Key="SpecialDatePicker" TargetType="DatePicker">
        <Border BorderThickness="TemplateBinding Border.BorderThickness" Padding="TemplateBinding Control.Padding" BorderBrush="TemplateBinding Border.BorderBrush" Background="TemplateBinding Panel.Background">
            <Grid Name="PART_Root" HorizontalAlignment="TemplateBinding Control.HorizontalContentAlignment" VerticalAlignment="TemplateBinding Control.VerticalContentAlignment">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.Resources>
                    <SolidColorBrush x:Key="DisabledBrush">#A5FFFFFF</SolidColorBrush>
                    <ControlTemplate TargetType="Button" x:Key="DropDownButtonTemplate">
                        <Grid>
                            <Grid Background="#11FFFFFF" Width="19" Height="18" FlowDirection="LeftToRight" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="20*" />
                                    <ColumnDefinition Width="20*" />
                                    <ColumnDefinition Width="20*" />
                                    <ColumnDefinition Width="20*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="23*" />
                                    <RowDefinition Height="19*" />
                                    <RowDefinition Height="19*" />
                                    <RowDefinition Height="19*" />
                                </Grid.RowDefinitions>
                                <Border BorderThickness="1,1,1,1" CornerRadius="0,0,1,1" BorderBrush="#FF45D6FA" Name="Highlight" Margin="-1,-1,-1,-1" Opacity="0" Grid.Row="0" Grid.ColumnSpan="4" Grid.RowSpan="4" />
                                <Border BorderThickness="1,1,1,1" CornerRadius="0.5,0.5,0.5,0.5" BorderBrush="#FFFFFFFF" Background="#FF1F3B53" Name="Background" Margin="0,-1,0,0" Opacity="1" Grid.Row="1" Grid.ColumnSpan="4" Grid.RowSpan="3" />
                                <Border BorderThickness="1,1,1,1" CornerRadius="0.5,0.5,0.5,0.5" BorderBrush="#BF000000" Name="BackgroundGradient" Margin="0,-1,0,0" Opacity="1" Grid.Row="1" Grid.ColumnSpan="4" Grid.RowSpan="3">
                                    <Border.Background>
                                        <LinearGradientBrush StartPoint="0.7,0" EndPoint="0.7,1">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#FFFFFFFF" Offset="0" />
                                                <GradientStop Color="#F9FFFFFF" Offset="0.375" />
                                                <GradientStop Color="#E5FFFFFF" Offset="0.625" />
                                                <GradientStop Color="#C6FFFFFF" Offset="1" />
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                </Border>
                                <Rectangle StrokeThickness="1" Grid.ColumnSpan="4" Grid.RowSpan="1">
                                    <Rectangle.Fill>
                                        <LinearGradientBrush StartPoint="0.46,1.6" EndPoint="0.3,-1.1">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#FF4084BD" />
                                                <GradientStop Color="#FFAFCFEA" Offset="1" />
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                    <Rectangle.Stroke>
                                        <LinearGradientBrush StartPoint="0.48,1.25" EndPoint="0.48,-1">
                                            <LinearGradientBrush.GradientStops>
                                                <GradientStop Color="#FF494949" />
                                                <GradientStop Color="#FF9F9F9F" Offset="1" />
                                            </LinearGradientBrush.GradientStops>
                                        </LinearGradientBrush>
                                    </Rectangle.Stroke>
                                </Rectangle>
                                <Path Data="M11.426758,8.4305077L11.749023,8.4305077 11.749023,16.331387 10.674805,16.331387 10.674805,10.299648 9.0742188,11.298672 9.0742188,10.294277C9.4788408,10.090176 9.9094238,9.8090878 10.365967,9.4510155 10.82251,9.0929432 11.176106,8.7527733 11.426758,8.4305077z M14.65086,8.4305077L18.566387,8.4305077 18.566387,9.3435936 15.671368,9.3435936 15.671368,11.255703C15.936341,11.058764 16.27293,10.960293 16.681133,10.960293 17.411602,10.960293 17.969301,11.178717 18.354229,11.615566 18.739157,12.052416 18.931622,12.673672 18.931622,13.479336 18.931622,15.452317 18.052553,16.438808 16.294415,16.438808 15.560365,16.438808 14.951641,16.234707 14.468243,15.826504L14.881817,14.929531C15.368796,15.326992 15.837872,15.525723 16.289043,15.525723 17.298809,15.525723 17.803692,14.895514 17.803692,13.635098 17.803692,12.460618 17.305971,11.873379 16.310528,11.873379 15.83071,11.873379 15.399232,12.079271 15.016094,12.491055L14.65086,12.238613z" Stretch="Fill" Fill="#FF2F2F2F" Margin="4,3,4,3" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="4" Grid.RowSpan="3" />
                                <Ellipse Fill="#FFFFFFFF" StrokeThickness="0" Width="3" Height="3" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.ColumnSpan="4" />
                                <Border BorderThickness="1,1,1,1" CornerRadius="0,0,0.5,0.5" BorderBrush="#B2FFFFFF" Name="DisabledVisual" Opacity="0" Grid.Row="0" Grid.ColumnSpan="4" Grid.RowSpan="4" />
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Grid.Resources>
                <Button Foreground="TemplateBinding TextElement.Foreground" Name="PART_Button" Width="20" Margin="3,0,3,0" HorizontalAlignment="Left" VerticalAlignment="Top" Focusable="False" Grid.Column="1" Grid.Row="0">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Grid>
                                <Grid Background="#11FFFFFF" Width="19" Height="18" FlowDirection="LeftToRight" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="20*" />
                                        <ColumnDefinition Width="20*" />
                                        <ColumnDefinition Width="20*" />
                                        <ColumnDefinition Width="20*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="23*" />
                                        <RowDefinition Height="19*" />
                                        <RowDefinition Height="19*" />
                                        <RowDefinition Height="19*" />
                                    </Grid.RowDefinitions>
                                    <Border BorderThickness="1,1,1,1" CornerRadius="0,0,1,1" BorderBrush="#FF45D6FA" Name="Highlight" Margin="-1,-1,-1,-1" Opacity="0" Grid.Row="0" Grid.ColumnSpan="4" Grid.RowSpan="4" />
                                    <Border BorderThickness="1,1,1,1" CornerRadius="0.5,0.5,0.5,0.5" BorderBrush="#FFFFFFFF" Background="#FF1F3B53" Name="Background" Margin="0,-1,0,0" Opacity="1" Grid.Row="1" Grid.ColumnSpan="4" Grid.RowSpan="3" />
                                    <Border BorderThickness="1,1,1,1" CornerRadius="0.5,0.5,0.5,0.5" BorderBrush="#BF000000" Name="BackgroundGradient" Margin="0,-1,0,0" Opacity="1" Grid.Row="1" Grid.ColumnSpan="4" Grid.RowSpan="3">
                                        <Border.Background>
                                            <LinearGradientBrush StartPoint="0.7,0" EndPoint="0.7,1">
                                                <LinearGradientBrush.GradientStops>
                                                    <GradientStop Color="#FFFFFFFF" Offset="0" />
                                                    <GradientStop Color="#F9FFFFFF" Offset="0.375" />
                                                    <GradientStop Color="#E5FFFFFF" Offset="0.625" />
                                                    <GradientStop Color="#C6FFFFFF" Offset="1" />
                                                </LinearGradientBrush.GradientStops>
                                            </LinearGradientBrush>
                                        </Border.Background>
                                    </Border>
                                    <Rectangle StrokeThickness="1" Grid.ColumnSpan="4" Grid.RowSpan="1">
                                        <Rectangle.Fill>
                                            <LinearGradientBrush StartPoint="0.46,1.6" EndPoint="0.3,-1.1">
                                                <LinearGradientBrush.GradientStops>
                                                    <GradientStop Color="#FF4084BD" />
                                                    <GradientStop Color="#FFAFCFEA" Offset="1" />
                                                </LinearGradientBrush.GradientStops>
                                            </LinearGradientBrush>
                                        </Rectangle.Fill>
                                        <Rectangle.Stroke>
                                            <LinearGradientBrush StartPoint="0.48,1.25" EndPoint="0.48,-1">
                                                <LinearGradientBrush.GradientStops>
                                                    <GradientStop Color="#FF494949" />
                                                    <GradientStop Color="#FF9F9F9F" Offset="1" />
                                                </LinearGradientBrush.GradientStops>
                                            </LinearGradientBrush>
                                        </Rectangle.Stroke>
                                    </Rectangle>
                                    <Path Data="M11.426758,8.4305077L11.749023,8.4305077 11.749023,16.331387 10.674805,16.331387 10.674805,10.299648 9.0742188,11.298672 9.0742188,10.294277C9.4788408,10.090176 9.9094238,9.8090878 10.365967,9.4510155 10.82251,9.0929432 11.176106,8.7527733 11.426758,8.4305077z M14.65086,8.4305077L18.566387,8.4305077 18.566387,9.3435936 15.671368,9.3435936 15.671368,11.255703C15.936341,11.058764 16.27293,10.960293 16.681133,10.960293 17.411602,10.960293 17.969301,11.178717 18.354229,11.615566 18.739157,12.052416 18.931622,12.673672 18.931622,13.479336 18.931622,15.452317 18.052553,16.438808 16.294415,16.438808 15.560365,16.438808 14.951641,16.234707 14.468243,15.826504L14.881817,14.929531C15.368796,15.326992 15.837872,15.525723 16.289043,15.525723 17.298809,15.525723 17.803692,14.895514 17.803692,13.635098 17.803692,12.460618 17.305971,11.873379 16.310528,11.873379 15.83071,11.873379 15.399232,12.079271 15.016094,12.491055L14.65086,12.238613z" Stretch="Fill" Fill="#FF2F2F2F" Margin="4,3,4,3" HorizontalAlignment="Center" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="4" Grid.RowSpan="3" />
                                    <Ellipse Fill="#FFFFFFFF" StrokeThickness="0" Width="3" Height="3" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.ColumnSpan="4" />
                                    <Border BorderThickness="1,1,1,1" CornerRadius="0,0,0.5,0.5" BorderBrush="#B2FFFFFF" Name="DisabledVisual" Opacity="0" Grid.Row="0" Grid.ColumnSpan="4" Grid.RowSpan="4" />
                                </Grid>
                            </Grid>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
                <!-- THIS LINE :: Notice the binding for Text -->
                <DatePickerTextBox  Text="Binding RelativeSource=RelativeSource AncestorType=DatePicker,Path=Text,Converter=StaticResource DateFixer" Foreground="TemplateBinding TextElement.Foreground" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Name="PART_TextBox" Grid.Column="0" Grid.Row="0" xml:space="preserve"></DatePickerTextBox>
                <Grid Name="PART_DisabledVisual" Opacity="0" IsHitTestVisible="False" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Rectangle RadiusX="1" RadiusY="1" Fill="#A5FFFFFF" Grid.Column="0" Grid.Row="0" />
                    <Rectangle RadiusX="1" RadiusY="1" Fill="#A5FFFFFF" Width="19" Height="18" Margin="3,0,3,0" Grid.Column="1" Grid.Row="0" />
                    <Popup Placement="Bottom" StaysOpen="False" AllowsTransparency="True" Name="PART_Popup" />
                </Grid>
            </Grid>
        </Border>
    </ControlTemplate>

【讨论】:

以上是关于从 DatePicker 以 yyyy/MM/dd 格式获取日期的主要内容,如果未能解决你的问题,请参考以下文章

NSJSONSerialization 将日期分隔符从 yyyy/MM/dd 更改为 yyyy\/MM\/dd [重复]

如何在Java中以字符串格式获取当前时间戳? “yyyy.MM.dd.HH.mm.ss”

Angular Datepicker更改日期格式

yyyy/mm/dd hh:mm:ss 用js怎么转成yyyy-mm-dd

Java 出力Excel 文件,并向该文件中写固定格式的日期。我往文件里写的是yyyy-mm-dd

如何将 yyyy/MM/dd hh:mm:ss 转换为秒?