Monday, December 28, 2015

Date time discussion

Finally i found how to convert time stamp to Date & Date to time stamp . I found some places in project people keep date as time stamp for get difference quickly. so in this case they use to keep the table column as Int or time stamp. now the problem is that in the application while showing the data, you need to convert it into date variable. So for that we can use the following code to convert time stamp to Date
 
int ts = 1451174400;
DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(ts).ToLocalTime();
string formattedDate = dt.ToString("dd-MM-yyyy");
 
Now you can get any date format from this variable.
In the second case if you want to convert Date to time stamp then check the following code.

int ts = (dt.Ticks - 621356166000000000) / 10000000;
Where dt is the date time variable & holding a date value.

If you want to convert string to date find the following code.
DateTime dt = Convert.ToDateTime("2015-12-13");

No comments:

Post a Comment