Created
July 17, 2018 13:53
-
-
Save rwhitmire/a52b0ec4558d0da61e1e2ddb919c805a to your computer and use it in GitHub Desktop.
date helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateHelpers | |
{ | |
public static DateTime GetDayStart(DateTime date, string timezone) | |
{ | |
var timezoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone); | |
var timezoneNow = TimeZoneInfo.ConvertTimeFromUtc(date, timezoneInfo); | |
return TimeZoneInfo.ConvertTimeToUtc(timezoneNow.Date, timezoneInfo); | |
} | |
public static DateTime GetWeekStart(DateTime date, string timezone) | |
{ | |
var timezoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone); | |
var timezoneNow = TimeZoneInfo.ConvertTimeFromUtc(date, timezoneInfo); | |
var diff = (7 + (timezoneNow.DayOfWeek - DayOfWeek.Sunday)) % 7; | |
var weekStartUtc = timezoneNow.AddDays(-1 * diff).Date; | |
return TimeZoneInfo.ConvertTimeToUtc(weekStartUtc, timezoneInfo); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment