Time function

  • Thread starter Thread starter nick.stefanov
  • Start date Start date
N

nick.stefanov

Is there a time fuction in C# similar to ctime in C that can convert to
a calendar date and time from seconds from January 1, 1970. Thanks.

Nick
 
Is there a time fuction in C# similar to ctime in C that can convert to
a calendar date and time from seconds from January 1, 1970. Thanks.

The simplest way to do that would be:

DateTime baseTime = new DateTime (1970, 1, 1);
DateTime date = baseTime.AddSeconds(seconds);
 
It's not quite the same thing -- ctime converts a time in seconds since
1/1/1970 12:00 AM GMT to a local time string. To do the same thing in
C#, you need to convert to local time before printing the date:

string ctime(int seconds /* since 12:00AM GMT 1/1/1970 */ )
{
DateTime baseTime = new DateTime (1970, 1, 1);
return baseTime.AddSeconds(seconds).ToLocalTime()
.ToString("ddd MMM dd hh:mm:ss yyyy");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top