Created
November 3, 2015 12:23
-
-
Save fcojperez/7ece14da8028acd704dc to your computer and use it in GitHub Desktop.
C# how to convert Date to Epoch
This file contains hidden or 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
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