10 examples of 'convert timestamp to date js' in JavaScript

Every line of 'convert timestamp to date js' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
3export function timestampToDate(timestamp?: pb.google.protobuf.ITimestamp) {
4 let millis: number
5 if (!timestamp) {
6 millis = 0
7 } else {
8 millis = timestamp.seconds as number * 1e3 + timestamp.nanos / 1e6
9 }
10 return new Date(millis)
11}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

66function dateConverter(timestamp, format) {
67
68 //JS uses nanoseconds, hence the *100 multiplication
69 var a = new Date(timestamp * 1000);
70
71 //short names of all twelve months
72 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
73
74 //thee variables used for getting timestamp time
75 var hrs = a.getHours(),
76 min = a.getMinutes(),
77 sec = a.getSeconds();
78
79 //improving readability: adding leading zero
80 if (hrs === 0) {
81 hrs = "0" + hrs;
82 }
83 if (min < 10) {
84 min = "0" + min;
85 }
86 if (sec < 10) {
87 sec = "0" + sec;
88 }
89
90 if (!format) {
91 //returns in format 'd M Y, H:i:s'
92 return a.getDate() + ' ' +
93 months[a.getMonth()] + ' ' +
94 a.getFullYear() + ', ' +
95 hrs + ':' + min + ':' + sec;
96
97 } else if (format === "time") {
98 return hrs + ':' + min + ':' + sec;
99 }
100
101}
78export function toDate(stamp: Time): Date {
79 const { sec, nsec } = stamp;
80 return new Date(sec * 1000 + nsec / 1e6);
81}
90export function timeStampDate(timeStamp) {
91
92 if (typeof timeStamp === "string") {
93 return timeStamp.replace(/T.+/, "");
94 }
95
96 return timeStamp;
97}
31static getDate(timestamp: number): string {
32 return Moment.unix(timestamp).format('DD.MM.YYYY');
33}
13getTimestamp (date) {
14 return new Date(
15 date.getFullYear(),
16 date.getMonth(),
17 date.getDate(),
18 this.time.hour,
19 this.time.min,
20 0, 0)
21}
45export function getDateFromTimestamp(milliseconds) {
46 return moment.utc(parseInt(milliseconds, 10)).toDate();
47}
101function timestamp2str(ts) {
102 const date = new Date(ts);
103 const y = date.getFullYear().toString();
104 const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
105 const m = month[date.getMonth()]
106 const d = date.getDate().toString();
107 return d + " " + m + " " + y;
108}
33function time_converter(UNIX_timestamp) {
34 var a = new Date(UNIX_timestamp * 1000);
35 var year = a.getFullYear();
36 var month = a.getMonth() + 1;
37 var date = a.getDate();
38 var hour = a.getHours();
39 var min = a.getMinutes();
40 var sec = a.getSeconds();
41 var time =
42 year +
43 '-' +
44 pad(month, 2) +
45 '-' +
46 pad(date, 2) +
47 ' ' +
48 pad(hour, 2) +
49 ':' +
50 pad(min, 2) +
51 ':' +
52 pad(sec, 2);
53 return time;
54}
9export function toDate () {
10 return new Date(this.valueOf());
11}

Related snippets