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

Every line of 'javascript convert date to 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
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}
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}
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}
78export function toDate(stamp: Time): Date {
79 const { sec, nsec } = stamp;
80 return new Date(sec * 1000 + nsec / 1e6);
81}
31static getDate(timestamp: number): string {
32 return Moment.unix(timestamp).format('DD.MM.YYYY');
33}
13function dateToDateTime(d) {
14 if (d) {
15 var date = moment(d);
16 // return null if not is valid
17 if (!date.isValid()) return null;
18 // return data in datetime format
19 return date.format('YYYY-MM-DD HH:mm:ss');
20 } else {
21 return null;
22 }
23}
90export function timeStampDate(timeStamp) {
91
92 if (typeof timeStamp === "string") {
93 return timeStamp.replace(/T.+/, "");
94 }
95
96 return timeStamp;
97}
41export function createDateTimeStamp() {
42 let result = '';
43 const date = new Date();
44 result =
45 date.getFullYear().toString() +
46 (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : (date.getMonth() + 1).toString()) +
47 (date.getDate() < 10 ? '0' + date.getDate().toString() : date.getDate().toString()) +
48 '_' +
49 date.getHours().toString() +
50 date.getMinutes().toString() +
51 date.getSeconds().toString();
52 return result;
53}
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}
45export function getDateFromTimestamp(milliseconds) {
46 return moment.utc(parseInt(milliseconds, 10)).toDate();
47}

Related snippets