Wednesday, January 8, 2014

Some Important Utility Functions in C#

//To get the UnixTimeMilliseconds
private static string GetUnixTimeMilliseconds()
{
 TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
 long timestamp = (long)t.TotalMilliseconds;
 return timestamp.ToString();
}
 
//To get the EasternTime
private static string GetEasternTime()
{ 
 string timestamp = string.Empty;
 var zone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
 var utcNow = DateTime.UtcNow;
 var easternNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, zone);
 timestamp = easternNow.ToString("yyyy-MM-dd HH':'mm':'ss");
 return timestamp;
}