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

  • Thread starter Thread starter Tony
  • Start date Start date
Something like:

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


?

Arne
 
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
 
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
 
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
 
Back
Top