How to: get Date, Time and Time zone from single DateTimePicker Control

前提是你 提交于 2019-12-11 18:57:36

问题


I am developing an application in which I am trying to fetch Date, Time and Time Zone from single DateTimePicker control. Can anybody help to resolve this?

Edit 1
I am using .NET Framework 2.0. Now I am able to fetch Date and Time with Single DateTimePicker by setting its CustomFormat property as

dtpicker2.CustomFormat = "MM/dd/yyyy,hh:mm:ss"

Now my problem is to fetch TimeZone.

Edit 2
Now here is one more issue, if I set DateTimePicker's format property to Time, it shows me something like 2:30:00 PM. I am storing this in a file and reassigning it to the DateTimePicker control.
Can anybody help me out, how to achieve this?


回答1:


You won't be able to do it using a DateTimePicker on it's own.

Rather, you are going to have to have sort of composite control which has a DateTimePicker on it, as well as another control which will serve as the offset.

This control could be a numeric up/down control which you would use for the hour, minute, second, etc, etc offset (or just a textbox for that matter).

Another option is if you have a location (in the form of latitude and longitude, which you can obtain via Google through HTTP if you have an address), to use the Timezone API function at GeoNames which will allow you to pass a latitude and longitude value and it will return the timezone for that location. That, along with the TZ database and the ZoneInfo project will allow you to determine the offset at that particular date/time (which isn't the same for all locations and all dates).




回答2:


Using the DateTimePicker you can change the Format Property to Custom, and specify the CustomFormat Property as dd MMM yyyy hh:mm:ss. This will allow you to in your code use

DateTime dt = dateTimePicker1.Value;
dt.Date
dt.TimeOfDay

Unfortunately the DateTimePicker does not have the built in TimeZone but you can make use of

TimeZone tz = TimeZone.CurrentTimeZone;

Also have a look at this Question

Custom Formating a DateTimePicker using CultureInfo




回答3:


The Any+Time(TM) Datepicker/Timepicker AJAX Calendar Widget now supports time zone input/output, selection and conversion. It should be compatible with .NET (see the Troubleshooting section on the web page if you have problems) and has pretty extensive timezone support (see the section on the web page about UTC offsets). Hopefully it will meet your needs.



来源:https://stackoverflow.com/questions/1978581/how-to-get-date-time-and-time-zone-from-single-datetimepicker-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!