Last active
January 28, 2023 03:57
-
-
Save marlun78/1347789 to your computer and use it in GitHub Desktop.
C# to/from JavaScript date converter
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
/** | |
* C# <> JavaScript Date Converter | |
* Copyright (c) 2011 marlun78 | |
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83 | |
*/ | |
/// <summary> | |
/// Provides a Convert method to convert between C# DateTime values and JavaScript parsable Int64 date values. | |
/// </summary> | |
public static class JavaScriptDateConverter | |
{ | |
// | |
private static DateTime _jan1st1970 = new DateTime(1970, 1, 1); | |
/// <summary> | |
/// Converts a DateTime into a (JavaScript parsable) Int64. | |
/// </summary> | |
/// <param name="from">The DateTime to convert from</param> | |
/// <returns>An integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.</returns> | |
public static long Convert (DateTime from) | |
{ | |
return System.Convert.ToInt64((from - _jan1st1970).TotalMilliseconds); | |
} | |
/// <summary> | |
/// Converts a (JavaScript parsable) Int64 into a DateTime. | |
/// </summary> | |
/// <param name="from">An integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.</param> | |
/// <returns>The date as a DateTime</returns> | |
public static DateTime Convert (long from) | |
{ | |
return _jan1st1970.AddMilliseconds(from); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work!