The Conversion of DateTime format in to seconds is called Unix Timestamp
The Timestamp count starts from DateTime Of 1970-1-1 00:00:00, which is called Unix Epoch.
using System;
namespace UnixAndJavaTimeStampFormat
{
class Program
{
static void Main(string[] args)
{
//Previous datetime based on Current datetime
int limittime =
10;
DateTime
currentdatetime = DateTime.Now;
DateTime beforeDateTime
= currentdatetime.AddDays(-limittime); // Substract days based on given limit, if minus(-) is
given, otherwise it Adds no. of Days
// DateTime beforeDateTime = currentdatetime.AddMinutes(-limittime);
/// for seconds
DateTime sTime = new DateTime(1970, 1, 1, 0,
0, 0, DateTimeKind.Utc);
long unixTimestamp=
(long)(beforeDateTime - sTime).TotalSeconds;
Console.WriteLine("Unix Timestamp Format In Seconds : " + unixTimestamp);
double timestamp =
unixTimestamp;
DateTime dateTimeformat
= new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTimeformat =
dateTimeformat.AddSeconds(timestamp);
Console.WriteLine("Date Time Format For Seconds : "+ dateTimeformat);
/// for
milliseconds
double
unixTimestampmilliseconds = (long)(beforeDateTime - sTime).TotalMilliseconds;
Console.WriteLine("Unix Timestamp Format In Milliseconds : " + unixTimestampmilliseconds);
double milliTimestamp
= unixTimestampmilliseconds;
DateTime
millidateTimeformat = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
millidateTimeformat =
millidateTimeformat.AddMilliseconds(milliTimestamp);
Console.WriteLine("Date Time Format for Milliseconds : " + millidateTimeformat);
Console.ReadKey();
}
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.