UnixTime (edit)
DateTimeOffset.ToUnixTimeMilliseconds Method (System) | Microsoft Docs
How to get the unix timestamp in C# - Stack Overflow
How To Convert DateTime To UnixTimeStamp in C# (tutorialsrack.com)
using System; namespace ConsoleApp1 { public static class DateTimeHelper { public static long GetUnixTimeMilliseconds() { return DateTime.UtcNow.ToUnixTimeMilliseconds(); } public static long GetUnixTimeSeconds() { return DateTime.UtcNow.ToUnixTimeSeconds(); } public static long ConvertToUnixTimeMilliseconds() { return DateTime.UtcNow.ConvertToUnixTimeMilliseconds(); } public static long ConvertToUnixTimeSeconds() { return DateTime.UtcNow.ConvertToUnixTimeSeconds(); } /// <summary> /// Using DateTimeOffset /// </summary> /// <param name="date"></param> /// <returns></returns> public static long ConvertToUnixTimeMilliseconds(this DateTime date) { var dateTimeOffset = new DateTimeOffset(date); var unixDateTime = dateTimeOffset.ToUnixTimeMilliseconds(); return unixDateTime; } /// <summary> /// Using DateTimeOffset /// </summary> /// <param name="date"></param> /// <returns></returns> public static long ConvertToUnixTimeSeconds(this DateTime date) { var dateTimeOffset = new DateTimeOffset(date); var unixDateTime = dateTimeOffset.ToUnixTimeSeconds(); return unixDateTime; } /// <summary> /// Using .ToUniversalTime().Subtract /// </summary> /// <param name="date"></param> /// <returns></returns> public static long ToUnixTimeMilliseconds(this DateTime date) { return (long)(date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalMilliseconds; } /// <summary> /// Using .ToUniversalTime().Subtract /// </summary> /// <param name="date"></param> /// <returns></returns> public static long ToUnixTimeSeconds(this DateTime date) { return (long)(date.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds; } } public class Program { private static void Main(string[] args) { long unixTimestamp = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds; Console.WriteLine($"UnixTimestamp: \t\t\t{unixTimestamp}"); Console.WriteLine($"GetUnixTimeMilliseconds: \t{DateTimeHelper.GetUnixTimeMilliseconds()}"); Console.WriteLine($"ConvertToUnixTimeMilliseconds: \t{DateTimeHelper.ConvertToUnixTimeMilliseconds()}"); Console.WriteLine($"GetUnixTimeSeconds: \t\t{DateTimeHelper.GetUnixTimeSeconds()}"); Console.WriteLine($"ConvertToUnixTimeSeconds: \t{DateTimeHelper.ConvertToUnixTimeSeconds()}"); } } }