Skip to content

Instantly share code, notes, and snippets.

@fcojperez
Created November 3, 2015 12:23
Show Gist options
  • Save fcojperez/7ece14da8028acd704dc to your computer and use it in GitHub Desktop.
Save fcojperez/7ece14da8028acd704dc to your computer and use it in GitHub Desktop.
C# how to convert Date to Epoch
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConvertDateTimeEpoch
{
class ConvertDateToEpoch
{
static void Main(string[] args)
{
ConvertDateToEpoch program = new ConvertDateToEpoch();
string fecha = "31/12/2015";
Console.WriteLine(String.Format("Fecha: {0}, Fecha Epoch: {1}", fecha, program.convertDateToEpoch(fecha)));
}
public decimal convertDateToEpoch(string stringDate)
{
decimal result = 0;
try
{
DateTime dt = DateTime.Parse(stringDate);
TimeSpan ts = dt - new DateTime(1970, 1, 1);
result = (decimal) ts.TotalMilliseconds;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment