10 examples of 'javascript timestamp to string' in JavaScript

Every line of 'javascript timestamp to string' 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
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}
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

62function timestamp(str){
63 return new Date(str).getTime();
64}
612function timestamp() {
613 var d = new Date();
614 var time = [pad(d.getHours()),
615 pad(d.getMinutes()),
616 pad(d.getSeconds())
617 ].join(':');
618 return [d.getDate(), months[d.getMonth()], time].join(' ');
619}
138function formatTimestamp(timestamp) {
139 return (new Date(Date.UTC(2014, 2, 22, 22, 22, 0, 0) + timestamp * 1000)).toLocaleString();
140}
49function timestamp() {
50 var date = new Date(Date.now());
51 var timestamp =
52 pad(date.getDate().toString()) +
53 pad((date.getMonth() + 1).toString()) +
54 date.getFullYear().toString().substr(2, 3) +
55 '_' +
56 pad(date.getHours().toString()) +
57 pad(date.getMinutes().toString()) +
58 pad(date.getSeconds().toString()) +
59 '_' +
60 process.pid;
61 return timestamp;
62}
520function timeStamp() {
521// Create a date object with the current time
522 var now = new Date();
523
524 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
525
526// Create an array with the current month, day and time
527 var date = [ months[now.getMonth()], now.getDate() + '-', now.getFullYear() ];
528
529// Create an array with the current hour, minute and second
530 var time = [ now.getHours(), now.getMinutes() ];
531
532// Determine AM or PM suffix based on the hour
533 var suffix = ( time[0] < 12 ) ? "AM" : "PM";
534
535// Convert hour from military time
536 time[0] = ( time[0] < 12 ) ? time[0] : time[0] - 12;
537
538// If hour is 0, set it to 12
539 time[0] = time[0] || 12;
540
541// If seconds and minutes are less than 10, add a zero
542 for ( var i = 1; i < 3; i++ ) {
543 if ( time[i] < 10 ) {
544 time[i] = "0" + time[i];
545 }
546 }
547
548// Return the formatted string
549 return date.join("") + "-" + time.join(".") + "" + suffix;
550}
170function iso8601TimestampToDate(string)
171{
172 var match = string.match(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)(?:\.(\d+))?(?:([+-])(\d+):(\d+))?/)
173 var date = Date.UTC(match[1], +match[2]-1 || 0, match[3] || 1,
174 match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0);
175 if (match[8])
176 date += (match[8] == "+" ? -1 : 1) *
177 ((+match[9] || 0)*60 + (+match[10] || 0))*60*1000;
178
179 return new Date(date);
180}
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}
51getMilliseconds() {
52 let ms = this.timestamp.milliseconds()
53 ms = ms.toString()
54 if (ms.length === 2) {
55 ms = "0" + ms
56 } else if(ms.length === 1) {
57 ms = "00" + ms
58 }
59 return ms
60}
132function timeToString(ts) {
133 var d = new Date(ts);
134 return DAYS[d.getDay()]
135 + ", " + MONTHS[d.getMonth()]
136 + " " + d.getDate()
137 + ", " + d.getFullYear();
138}

Related snippets