Reputation: 23
I have two dates. Date1 and Date2
Date1 contains "10/12/2010 12:00:00AM". Date2 contains "10/10/2010 03:00:00PM"
I want to extract only the date from Date1 and extract only the time from Date2 and join them.
Example "10/12/2010 03:00:00PM".
Any ideas?
Thanks
Upvotes: 2
Views: 776
Reputation: 113242
If you are going to convert to UTC, you probably want to do so with both dates, for cases where the timezone difference crosses into another date. Maybe you don't for reasons that aren't clear, in which case remove the relevant conversion from below:
date1.ToUniversalTime().Date + date2.ToUniversalTime().TimeOfDay
This also assumes the dates are both known to be in local time. You may wish to add a check of the Kind
property:
(date1.Kind == DateTimeKind.Utc ? date1 : date1.ToUniversalTime()).Date + (date2.Kind == DateTimeKind.Utc ? date2 : date2.ToUniversalTime()).TimeOfDay
Upvotes: 1
Reputation: 137148
DateTime.Date
will get you the date part and DateTime.TimeOfDay
will get you the time part, so:
DateTime date3 = date1.Date + date2.TimeOfDay;
should do what you want.
Upvotes: 2
Reputation: 269368
DateTime date3 = date1.Date + date2.TimeOfDay;
Where does UTC come into it?
Upvotes: 3