10 examples of 'javascript convert date to utc timestamp' in JavaScript

Every line of 'javascript convert date to utc timestamp' 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
21export function dateToUTC(timestamp) {
22 const date = new Date(timestamp * 1000);
23 return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),
24 date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
25}
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

5export function dateUTCToLocal( date )
6{
7 return new Date( date.getTime( ) + TimeZoneOffset );
8}
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}
4function treatAsUtc(dateStr) {
5 let result = new Date(dateStr);
6 result.setMinutes(result.getMinutes() - result.getTimezoneOffset());
7 return result;
8}
78export function toDate(stamp: Time): Date {
79 const { sec, nsec } = stamp;
80 return new Date(sec * 1000 + nsec / 1e6);
81}
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}
45export function getDateFromTimestamp(milliseconds) {
46 return moment.utc(parseInt(milliseconds, 10)).toDate();
47}
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}
17function datetime_to_datetime(d)
18{
19 var date = new Date(d);
20 var now_utc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(),
21 date.getUTCDate(), date.getUTCHours(),
22 date.getUTCMinutes(), date.getUTCSeconds());
23
24 return new Date(now_utc);
25}
127function toUnixTimestamp(date) {
128 return Math.floor(date.getTime() / 1000);
129}

Related snippets