Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Ground Controller Lvl 1
Message 1 of 2

How to convert a TimeStamp to date time

hi, 

I need to convert a TimeStamp 1549357587578 in DataTime 10:08:04:557- 5.2.2019.
How would I do this in Visual Studio Code?
Thank You!
 
1 Reply
Commander Lvl 1
Message 2 of 2

Re: How to convert a TimeStamp to date time

Hi @jeta84,

 

Welcome to the GitHub Community forum!

 

The editor is not a relevant information, in which programming language?

 

I can list some languages to do what you want:

 

In python 3.x (even on python 2.x i think) you can use the method of datetime module -> utcfromtimestamp than you need the right format using date.strftime method with the right directive -> strftime-strptime-behavior:

print(datetime.utcfromtimestamp(1370001284000).strftime("%Y-%m-%d %H:%M:%S"))

 

In Java you can use built-in function:

Timestamp timestamp = new Timestamp(1549357587578);
Date date = new Date(timestamp.getTime());

 

In Javascript you can use momentjs format, the code need to be something like this:

var date = new Date(1370001284000);
var format = date.format("dd.mm.yyyy hh:MM:ss"); 

 

In C++ use the localtime function -> ctime::localtime and if you follow the example you can extract something like this:

/* localtime example */
#include <stdio.h>      /* puts, printf */
#include <time.h>       /* time_t, struct tm, time, localtime */

int main ()
{
  time_t rawtime  = (const time_t)1370001284000;
  struct tm * timeinfo;

  timeinfo = localtime (&rawtime);
  printf ("Current local time and date: %s", asctime(timeinfo));

  return 0;
}

 

In C#  you need to do some trick:

public DateTime UnixTimestampToDateTime(long _UnixTimeStamp)
{
    return (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(_UnixTimeStamp);
}

 

Hope this helps 🙂  

 

-Gabriele-

 

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!