Created
March 1, 2018 20:08
-
-
Save neilgaietto/8139f9bd9b2fa3c1e1a739b2c847f08b to your computer and use it in GitHub Desktop.
DateTime Utilities
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
/// <summary> | |
/// DateTime for EST | |
/// </summary> | |
/// <returns>EST</returns> | |
public static DateTime LocalDateTime() | |
{ | |
var timeUtc = DateTime.UtcNow; | |
DateTime easternTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, EasternTimeZone()); | |
return easternTime; | |
} | |
/// <summary> | |
/// Convert a UTC time to EST time | |
/// </summary> | |
/// <returns>EST equivalent of a UTC time</returns> | |
public static DateTime ToLocalDateTime(this DateTimeOffset dateTime) | |
{ | |
return dateTime.DateTime.ToLocalDateTime(); | |
} | |
/// <summary> | |
/// Convert a UTC time to EST time | |
/// </summary> | |
/// <returns>EST equivalent of a UTC time</returns> | |
public static DateTime ToLocalDateTime(this DateTime dateTime) | |
{ | |
return TimeZoneInfo.ConvertTimeFromUtc(dateTime, EasternTimeZone()); | |
} | |
//Converts a DateTime object for an EST time to an offset object for EST | |
public static DateTimeOffset EstOffset(this DateTime dateTime) | |
{ | |
var converted = TimeZoneInfo.ConvertTime(dateTime, EasternTimeZone(), EasternTimeZone()); | |
var offset = new DateTimeOffset(converted, EasternTimeZone().GetUtcOffset(converted)); | |
return offset; | |
} | |
/// <summary> | |
/// Timezone for EST | |
/// </summary> | |
/// <returns></returns> | |
public static string EasternTimeZoneName() | |
{ | |
return "Eastern Standard Time"; | |
} | |
public static TimeZoneInfo EasternTimeZone() | |
{ | |
return TimeZoneInfo.FindSystemTimeZoneById(EasternTimeZoneName()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment