4

In c#, how can I convert seconds to date and time format.The date and time format is as follows. YYYY-MM-DD HH:MM:SS . I had time in seconds not in milliseconds. I want to convert 1365004800 to 2013-04-03 16:00:00

Divya
  • 211
  • 3
  • 10

2 Answers2

2

Try this below code

DateTime date = new DateTime(1970,1,1,0,0,0,0); //Set default date 1/1/1970
date = date.AddSeconds(1365004800); //add seconds
label1.Text = date.ToString(); //Display result 2013-04-03 16:00:00
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
1

So the C# to convert Unix Time, (which is what you have) to a date time is:

public static DateTime UnixTimeStampToNormalDateTime(double unixTimeStamp)
{
    System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0);
    dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
    return dtDateTime;
}

Then format the date accordingly with format in C#.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76