How would I convert a preexisting datetime to UTC time without changing the actual time.
Example:
DateTime dateTime = GetSomeDateTime(); // dateTime here is 3pm
dateTime.ToUtcDateTime() // datetime should still be 3pm
6/1/2011 4:08:40 PM Local
6/1/2011 4:08:40 PM Utc
from
DateTime dt = DateTime.Now;
Console.WriteLine("{0} {1}", dt, dt.Kind);
DateTime ut = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
Console.WriteLine("{0} {1}", ut, ut.Kind);
Use the DateTime.SpecifyKind static method.
Creates a new DateTime object that has the same number of ticks as the specified DateTime, but is designated as either local time, Coordinated Universal Time (UTC), or neither, as indicated by the specified DateTimeKind value.
Example:
DateTime dateTime = DateTime.Now;
DateTime other = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
Console.WriteLine(dateTime + " " + dateTime.Kind); // 6/1/2011 4:14:54 PM Local
Console.WriteLine(other + " " + other.Kind); // 6/1/2011 4:14:54 PM Utc
You can use the overloaded constructor of DateTime:
DateTime utcDateTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, DateTimeKind.Utc);
Use the DateTime.ToUniversalTime method.
Related
I want to check how it's possible to identify the difference that is more than 12 months from DateTimeOffset.
var startDate = DateTimeOffset.Parse("08/11/2012 12:00:00");
var endDate= DateTimeOffset.Parse("08/12/2013 13:00:00");
TimSpan tt = startDate - endDate;
In the timespan, there is no option for the month or year.
Instead of subtracting one from another to get a TimeSpan, add 12 months to the start to find out the cut-off:
if (startDate.AddMonths(12) > endDate)
{
// ...
}
Note that you should think carefully about corner cases - in particular, what you'd want to do with a start date of February 29th...
You can add or subtract either dates or time intervals from a particular DateTimeOffset value. Arithmetic operations with DateTimeOffset values, unlike those with DateTime values, adjust for differences in time offsets when returning a result. For example, the following code uses DateTime variables to subtract the current local time from the current UTC time. The code then uses DateTimeOffset variables to perform the same operation. The subtraction with DateTime values returns the local time zone's difference from UTC, while the subtraction with DateTimeOffset values returns TimeSpan.Zero.
using System;
public class DateArithmetic
{
public static void Main()
{
DateTime date1, date2;
DateTimeOffset dateOffset1, dateOffset2;
TimeSpan difference;
// Find difference between Date.Now and Date.UtcNow
date1 = DateTime.Now;
date2 = DateTime.UtcNow;
difference = date1 - date2;
Console.WriteLine("{0} - {1} = {2}", date1, date2, difference);
// Find difference between Now and UtcNow using DateTimeOffset
dateOffset1 = DateTimeOffset.Now;
dateOffset2 = DateTimeOffset.UtcNow;
difference = dateOffset1 - dateOffset2;
Console.WriteLine("{0} - {1} = {2}",
dateOffset1, dateOffset2, difference);
// If run in the Pacific Standard time zone on 4/2/2007, the example
// displays the following output to the console:
// 4/2/2007 7:23:57 PM - 4/3/2007 2:23:57 AM = -07:00:00
// 4/2/2007 7:23:57 PM -07:00 - 4/3/2007 2:23:57 AM +00:00 = 00:00:00
}
}
For more details you can read HERE
I have the following code that take in input scheduleConfiguration.Time in the UTC and return ticks in local time.
scheduleConfiguration.Time equal {9/13/2015 10:00:00 AM} in UTC
var localTime = scheduleConfiguration.Time.ToLocalTime(); {9/13/2015 1:00:00 PM} in Local
var executionTime = new TimeSpan(localTime.TimeOfDay.Ticks);
I changed my data contract scheduleConfiguration.Time, so I need to use TimeSpan TimeOfDay instead of DateTime Time, but I need to have the same executionTime. So I do next
var local time = DateTime.Now.Date.Add(scheduleConfiguration.TimeOfDay);//{9/13/2015 10:00:00 AM} in Local
var executionTime = new TimeSpan(localTime.Ticks);
So I have the difference in 3 hours (and I have UTC +3 time zone)
How to get the same result as for DateTime in the first situation?
You should use SpecifyKind method.
var time = DateTime.SpecifyKind(DateTime.Now.Date.Add(span), DateTimeKind.Utc);
Console.WriteLine(time.ToLocalTime());
I want to convert date entered int String to DateTime type
I use the following
DateTime date = Convert.ToDateTime(dateText);
Let say the dateText=5/20/2014 DateTime date become 5/20/2014 and 12:00:00 AM. I I want the current time of the day to be picked up instead of 12:00:00 AM.
Is there a workaround?
After you do:
DateTime date = Convert.ToDateTime(dateText);
Then do:
DateTime fullDate = new DateTime(date.Year, date.Month, date.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
...or as suggested by Ulugbek Umirov:
DateTime fullDate = date.Add(DateTime.Now.TimeOfDay);
Try this :
DateTime date = Convert.ToDateTime(dateText + " " + DateTime.Now.TimeOfDay);
I'm getting a time from a use as a string. This time is assumed to be in Eastern time. I need store this in the database as UTC time. How do I do this?
DateTime.SpecifyKind doesn't accept Eastern. In another thread, I read something about using DateTimeOffset
From MSDN:
DateTime easternTime = new DateTime(2007, 01, 02, 12, 16, 00);
string easternZoneId = "Eastern Standard Time";
try
{
TimeZoneInfo easternZone = TimeZoneInfo.FindSystemTimeZoneById(easternZoneId);
Console.WriteLine("The date and time are {0} UTC.",
TimeZoneInfo.ConvertTimeToUtc(easternTime, easternZone));
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("Unable to find the {0} zone in the registry.",
easternZoneId);
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("Registry data on the {0} zone has been corrupted.",
easternZoneId);
}
http://msdn.microsoft.com/en-us/library/bb397769.aspx
DateTime dateNow = DateTime.Now;
Console.WriteLine("The date and time are {0} UTC.",
TimeZoneInfo.ConvertTimeToUtc(dateNow));
i have a datetime(in utc) saved in database and i also know the utc offset in the following format.
-03:00:00
how to convert this to a DateTime
This simplest way to apply an "offset" to a DateTime that you already have is to create a TimeSpan structure which holds your offset value, and then simply "add" the offset to the original DateTime value.
For example:
DateTime utcDateTime = DateTime.Parse("29 July 2010 14:13:45");
TimeSpan offSet = TimeSpan.Parse("-03:00:00");
DateTime newDateTime = utcDateTime + offSet;
Console.WriteLine(newDateTime);
This results in the following output:
29/07/2010 11:13:45
which is the original time (29 July 2010 14:13:45) minus 3 hours (the offset - -03:00:00).
Note that this technique is merely performing simple arithmetic with your DateTime value and does not take any time zones into account.
The problem you are likely running into is that most DB drivers when fetching from the database will create the DateTime with DateTimeKind.Unspecified, which may not convert to UTC properly even when you use ToUniversalTime. To get arround this I use an extension method like this:
public static DateTime ToSafeUniversalTime(this DateTime date) {
if(date != DateTime.MinValue && date != DateTime.MaxValue) {
switch(date.Kind) {
case DateTimeKind.Unspecified:
date = new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, DateTimeKind.Utc);
break;
case DateTimeKind.Local:
date = date.ToUniversalTime();
break;
}
}
return date;
}