格式化datepicker日期 [英] Formatting datepicker date

查看:139
本文介绍了格式化datepicker日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我正在尝试在datepicker中格式化日期。我正在使用DisplayDate,它正确格式化日期,但它是在当前日期而不是在datepicker中选择的日期。我做错了什么?





Code



Hi everyone.

I am trying to format date in datepicker. I am using the DisplayDate and it is formatting the date correctly but it is taking in the current date and not the date selected in datepicker. What am I doing wrong?


Code

string date1=datePicker1.DisplayDate.ToString("yyyy-MM-dd");

推荐答案

默认显示日期是当前日期(DateTime) 。现在可以在xaml或代码中指定为日期选择器控件的属性。



如果你想获得日期和格式,你将会需要执行以下操作:



The Display date by default is the current date (DateTime.Now) and can be specified as an attribute on the date picker control in your xaml or within code.

If you are trying to get the date and format you will need to do something like the following:

<DatePicker HorizontalAlignment="Left" Margin="109,115,0,0" VerticalAlignment="Top" Name="DatePicker1"/>







string date1 = Convert.ToDateTime(DatePicker1.Text).ToString("yyyy-MM-dd");





当然你可以把这个转过来进入一个扩展方法或添加一些安全防护,所以如果你根本不选择日期,它不会爆炸。但这样您就可以根据需要设置所选日期的格式。



Of course you could turn this into an extension method or add some safe guards so it won''t blow up if you dont select a date at all. But that will allow you to format your selected date as you desire.


这篇关于格式化datepicker日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆