user
user

Reputation: 1759

Convert date and time to GMT format

I have individual calendar and time pickers. I want to combine both and convert to GMT time.

var time = timepicker.Time; //This is in 24hour format, but needs to store in AM,PM format
var date = calendar.SelectedDate;
string add_date_time = date+ " " +time;
DateTime gmt = add_date_time.ToUnviersalTime();

I get the error as

"cannot convert string to System.DateTime"

EDIT:

this is xml code for calendar and timepicker.

 <controls:Calendar  x:Name="calendar"/>
 <TimePicker x:Name="time_picker" Format = "T"/>

After converting to gmt, I want to store in DB.

Upvotes: 3

Views: 1930

Answers (1)

ProgrammingLlama
ProgrammingLlama

Reputation: 38767

Since calendar.SelectedDate is a DateTime? and timepicker.Time is a TimeSpan, the following code should work:

DateTime gmt = (calendar.SelectedDate.Value + timepicker.Time).ToUniversalTime();

You might want to check calendar.SelectedDate.HasValue is true before running this code, in case no date is selected.

You can then use this DateTime value with your database access code.

Upvotes: 4

Related Questions