Does it exist a function that return hh:mm or date for an input that is number of seconds for a give

A

Arne Vajhoej

Something like:

public static string Convert(int secs)
{
return DateTime.Now.Date.AddSeconds(secs).ToString("HH:mm:ss");
}


?

Arne
 
M

Marcel Müller

Something like:

public static string Convert(int secs)
{
return
DateTime.Now.Date.AddSeconds(secs).ToString("HH:mm:ss");
}

Maybe that does not hit the nail on the head.

A date in Seconds is usually a unix timestamp with seconds since epoch.

new DateTime(1970,1,1).AddSeconds(secs).ToString("yyyy-mm-dd HH:mm:ss");

should do this.


Marcel
 
A

Arne Vajhoej

Maybe that does not hit the nail on the head.

A date in Seconds is usually a unix timestamp with seconds since epoch.

That is very common. And it could very well be the case here as well.
I just did not see it as a good fit for this question.
new DateTime(1970,1,1).AddSeconds(secs).ToString("yyyy-mm-dd HH:mm:ss");

should do this.

Seconds since start of 1970 is usually since start of 1970 GMT, so
you would need to do something to handle timezones other than GMT.

Arne
 
A

Arne Vajhøj

That is very common. And it could very well be the case here as well.
I just did not see it as a good fit for this question.


Seconds since start of 1970 is usually since start of 1970 GMT, so
you would need to do something to handle timezones other than GMT.

Example:

using System;
using System.Runtime.InteropServices;

namespace E
{
public class Program
{
private static readonly DateTime epoch = new DateTime(1970, 1,
1, 0, 0, 0, DateTimeKind.Utc);
[DllImport("msvcrt.dll")]
public static extern long time(IntPtr t);
public static void Main(string[] args)
{
long t = time(IntPtr.Zero);
DateTime now = epoch.AddSeconds(t).ToLocalTime();
Console.WriteLine(now);
Console.ReadKey();
}
}
}

Arne
 

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

Top