I have a TimeZone received from customer and I use it to set expiration DateTime so that it is UTC equivalent of "End of Month" in customer's time zone.
I tried this:
var current = timezone.ToUniversalTime(DateTime.UtcNow);
But could not manage to work. Can anybody help me with that?
You can try like this:
DateTime utcTime = new DateTime(2016,2,10,10,15,00);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Your Time Zone");
var tzTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tz);
//To get time in UTC
var utcTime = TimeZoneInfo.ConvertTimeToUtc(tzTime, tz);
Related
I am working on a project that requires me to be comparing the date stored as string in database to the current date.
This particular application works in localhost but on live server, it seems to be picking the date of the server. I am resident in West Central Africa. How do i get the Date returned by my converted timezone in the format dd/MM/yyyy and not MM/dd/yyyy that it is seeing it.
Below is a code snippet.
var date = DateTime.UtcNow ;
var zone = TimeZoneInfo.FindSystemTimeZoneById("W. Central Africa Standard Time");
DateTime currentTime = TimeZoneInfo.ConvertTimeFromUtc(date, zone);
var loanDates = currentTime.Date;
var OldloanDate = loanDates.ToShortDateString();
var nloanDate = DateTime.ParseExact(OldloanDate, "dd/MM/yyyy", CultureInfo.InvariantCulture).Date;
var loanDate = nloanDate.ToShortDateString();
I want the loanDate to be in string form and of the format dd/MM/yyyy
You can try this:
var date = DateTime.UtcNow;
var zone = TimeZoneInfo.FindSystemTimeZoneById("W. Central Africa Standard Time");
var currentTime = TimeZoneInfo.ConvertTimeFromUtc(date, zone);
var loanDate = currentTime.Date.ToString("dd/MM/yyyy").Replace("-", "/");
I'm trying to use DateTimeOffset to convey a specific moment in time across any time zone. I can't figure out how to use TimeZoneInfo to deal with daylight saving time.
var dt = DateTime.UtcNow;
Console.WriteLine(dt.ToLocalTime());
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);
Console.WriteLine(utcOffset.ToOffset(tz.BaseUtcOffset));
This prints out:
6/2/2010 4:37:19 PM
6/2/2010 3:37:19 PM -06:00
I am in the central time zone, and and we are currently in daylight saving time.
I am trying to get the second line to read:
6/2/2010 4:37:19 PM -05:00
BaseUtcOffset apparently doesn't change based on DST.
How can I get the the right time with the proper offset value?
You can also use TimeZoneInfo.ConvertTimeFromUtc, which will allow for daylight saving time:
DateTime utc = DateTime.UtcNow;
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, zone);
You need to get the UtcOffset from the TimeZoneInfo, then pass that to the ToOffset() method:
var dt = DateTime.UtcNow;
Console.WriteLine(dt.ToLocalTime());
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);
Console.WriteLine(utcOffset.ToOffset(tz.GetUtcOffset(utcOffset)));
Or better, if you don't want to hard code the time zone identifier:
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id);
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
I'm a beginner both at .NET and stackoverflow, so I could be wrong, but here goes:
Using TimeZoneInfo.ConvertTimeFromUtc will allow for daylight saving time, and convert to the correct time according to the time zone + a possible DST offset. However - the offset itself in the resulting object will show the offset for standard time, and not take daylight saving time into account. So if you want to do a ToString on the object, you will end up with the correct time (in hours and minutes), but the wrong offset during daylight saving time, which may lead to the wrong moment in time later in the code.
If you instead use the GetUtcOffset to get the offset for a specific time, and then do a ToOffset on the DateTimeOffset object, both the hours/minutes and the offset itself will be correctly converted, and you can safely do a ToString.
string ExpectedDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss''zzz";
string timeZoneId = "FLE Standard Time";
string dateTimestr = "2017-10-09T09:00:00+02:00";
DateTimeOffset dto = DateTimeOffset.Parse(dateTimeStr);
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
TimeSpan offset = zone.GetUtcOffset(dto);
dto = dto.ToOffset(offset);
string localTime = dto.ToString(ExpectedDateTimePattern);
localTime will return "2017-10-09T10:00:00+03:00".
datetimeoffset timezoneinfo getutcoffset
This will Adjust automatically... and Return time as per your timezone.
public static string SetLastModified (
TimeZoneInfo csttzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
DateTime cstTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, csttzi);
return String.Format("DaylightSavingTime: {0}", cstTime.IsDaylightSavingTime().ToString());
}
I need to be able to convert UTC time to Local Time, add one hour and would like to convert back to UTC for comparison. However, the basic to TimeZone and back to UTC gives incorrect time.
DateTime utcDateTime = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Utc);
var localTime = utcDateTime.ToZone("Central Time (US & Canada)"); // CORRECT TIME
// INCORRECT when converted back to utc. It should be the same as utcDateTime
var convertedBack = TimeZoneInfo.ConvertTimeToUtc(localTime);
What am I missing?
// This works
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var convertedBack =TimeZoneInfo.ConvertTimeToUtc(localTime, tz);
I'm trying to use DateTimeOffset to convey a specific moment in time across any time zone. I can't figure out how to use TimeZoneInfo to deal with daylight saving time.
var dt = DateTime.UtcNow;
Console.WriteLine(dt.ToLocalTime());
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);
Console.WriteLine(utcOffset.ToOffset(tz.BaseUtcOffset));
This prints out:
6/2/2010 4:37:19 PM
6/2/2010 3:37:19 PM -06:00
I am in the central time zone, and and we are currently in daylight saving time.
I am trying to get the second line to read:
6/2/2010 4:37:19 PM -05:00
BaseUtcOffset apparently doesn't change based on DST.
How can I get the the right time with the proper offset value?
You can also use TimeZoneInfo.ConvertTimeFromUtc, which will allow for daylight saving time:
DateTime utc = DateTime.UtcNow;
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, zone);
You need to get the UtcOffset from the TimeZoneInfo, then pass that to the ToOffset() method:
var dt = DateTime.UtcNow;
Console.WriteLine(dt.ToLocalTime());
var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var utcOffset = new DateTimeOffset(dt, TimeSpan.Zero);
Console.WriteLine(utcOffset.ToOffset(tz.GetUtcOffset(utcOffset)));
Or better, if you don't want to hard code the time zone identifier:
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id);
DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, tzi);
I'm a beginner both at .NET and stackoverflow, so I could be wrong, but here goes:
Using TimeZoneInfo.ConvertTimeFromUtc will allow for daylight saving time, and convert to the correct time according to the time zone + a possible DST offset. However - the offset itself in the resulting object will show the offset for standard time, and not take daylight saving time into account. So if you want to do a ToString on the object, you will end up with the correct time (in hours and minutes), but the wrong offset during daylight saving time, which may lead to the wrong moment in time later in the code.
If you instead use the GetUtcOffset to get the offset for a specific time, and then do a ToOffset on the DateTimeOffset object, both the hours/minutes and the offset itself will be correctly converted, and you can safely do a ToString.
string ExpectedDateTimePattern = "yyyy'-'MM'-'dd'T'HH':'mm':'ss''zzz";
string timeZoneId = "FLE Standard Time";
string dateTimestr = "2017-10-09T09:00:00+02:00";
DateTimeOffset dto = DateTimeOffset.Parse(dateTimeStr);
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
TimeSpan offset = zone.GetUtcOffset(dto);
dto = dto.ToOffset(offset);
string localTime = dto.ToString(ExpectedDateTimePattern);
localTime will return "2017-10-09T10:00:00+03:00".
datetimeoffset timezoneinfo getutcoffset
This will Adjust automatically... and Return time as per your timezone.
public static string SetLastModified (
TimeZoneInfo csttzi = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
DateTime cstTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, csttzi);
return String.Format("DaylightSavingTime: {0}", cstTime.IsDaylightSavingTime().ToString());
}
I need to find what day it is for a user given a Timespan offset.
Right now, this is what I have tried:
string timezone = this.HttpContext.Request.Headers["X-MyApp-Timezone"];
var timeSpan = TimeSpan.Parse(timezone, DateTimeFormatInfo.InvariantInfo);
var consumersCurrentTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeSpan);
var consumersCurrentDate = consumersCurrentTime.Date();
Which I know doesn't work because TimeZoneInfo.ConvertTimeFromUtc takes a TimeZoneInfo not a TimeSpan. I looked into it a little and found that it's impossible given an offset to get the entire Time Zone information, but I really don't need to know the exact time zone, just what day it is. Is there any way for me to do this?
string timezone = this.HttpContext.Request.Headers["X-MyApp-Timezone"];
DateTime consumersCurrentDateTime = DateTime.UtcNow + TimeSpan.Parse(timezone,
DateTimeFormatInfo.InvariantInfo);
DateTime customersCurrentDate = consumersCurrentDateTime.Date;
Thanks to Joao Paulo's comment, I think I can do this without involving time zones at all, just by doing this:
string timezone = this.HttpContext.Request.Headers["X-MyApp-Timezone"];
var timeSpan = TimeSpan.Parse(timezone, DateTimeFormatInfo.InvariantInfo);
var consumersCurrentDate = DateTimeOffset.UtcNow.ToOffset(timeSpan).Date;