9 examples of 'javascript date yyyy mm dd' in JavaScript

Every line of 'javascript date yyyy mm dd' 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
40export function yyyymmdd(date) {
41 date = date || new Date()
42 return date.getFullYear() +
43 pad2(date.getMonth() + 1) +
44 pad2(date.getDate())
45}
64function dateToYYYYMMDD (date) {
65 if (date instanceof Date) {
66 return [
67 date.getFullYear(),
68 String(date.getMonth() + 1).padStart(2, 0),
69 String(date.getDate()).padStart(2, 0),
70 ].join("");
71 }
72
73 // TODO: better checking here?
74
75 return date;
76}
8static formatDateYYYYMMDD(date) {
9 return DateFormatter.format(date, "yyyy-MM-dd");
10}
33export function yyyymmddhhmmss(date) {
34 date = date || new Date()
35 return yyyymmdd(date) +
36 pad2(date.getHours()) +
37 pad2(date.getMinutes()) +
38 pad2(date.getSeconds())
39}
12export default function yyyyMMdd(validDate) {
13 let date;
14
15 if (!validDate) {
16 return "";
17 } else if (isyyyyMMdd(validDate)) {
18 date = validDate;
19 } else {
20 date = validDate.toISOString();
21 }
22
23 return date.slice(0, 10);
24}
129function dateFormat(d) {
130 let month = d.getMonth() + 1;
131 let date = d.getDate();
132 if (month < 10) month = `0${month}`;
133 if (date < 10) date = `0${date}`;
134 return `${d.getFullYear()}-${month}-${date}`;
135}
17function formatDate(d) {
18 function addZero(n) {
19 return n < 10 ? '0' + n : '' + n;
20 }
21
22 return d.getFullYear() + "-" + addZero(d.getMonth()+1)
23 + "-" + addZero(d.getDate()) + " " + addZero(d.getHours())
24 + ":" + addZero(d.getMinutes()) + ":" + addZero(d.getSeconds());
25}
11format (fmt = 'yyyy-MM-dd HH:mm:ss') {
12 const obj = {
13 'y+': this.date.getFullYear(),
14 'M{2}': this._paddingZero(this.date.getMonth() + 1),
15 'd{2}': this._paddingZero(this.date.getDate()),
16 'H{2}': this._paddingZero(this.date.getHours()),
17 'h{2}': this._paddingZero(this.date.getHours() % 12),
18 'm{2}': this._paddingZero(this.date.getMinutes()),
19 's{2}': this._paddingZero(this.date.getSeconds()),
20 'M': this.date.getMonth() + 1,
21 'd': this.date.getDate(),
22 'H': this.date.getHours(),
23 'h': this.date.getHours() % 12,
24 'm': this.date.getMinutes(),
25 's': this.date.getSeconds(),
26 'W': this.date.getDay()
27 }
28 for (let [key, ] of Object.entries(obj)) {
29 const regexp = new RegExp(`(${key})([^a-zA-Z])?`)
30 if (regexp.test(fmt)) {
31 fmt = fmt.replace(RegExp.$1, obj[key])
32 }
33 }
34 return fmt
35}
180export function dateFormat(timestamp, format = 'yyyy-MM-dd hh:mm:ss') {
181 timestamp = timestamp.length === 13 ? timestamp : timestamp * 1000
182 let date = new Date(timestamp)
183 let args = {
184 'M+': date.getMonth() + 1,
185 'd+': date.getDate(),
186 'h+': date.getHours(),
187 'm+': date.getMinutes(),
188 's+': date.getSeconds()
189 }
190 if (/(y+)/.test(format)) {
191 format = format.replace(
192 RegExp.$1,
193 (date.getFullYear() + '').substr(4 - RegExp.$1.length)
194 )
195 }
196 for (var i in args) {
197 let n = args[i]
198 if (new RegExp('(' + i + ')').test(format)) {
199 format = format.replace(
200 RegExp.$1,
201 RegExp.$1.length === 1 ? n : ('00' + n).substr(('' + n).length)
202 )
203 }
204 }
205 return format
206}

Related snippets