10 examples of 'javascript format date mm dd yyyy' in JavaScript

Every line of 'javascript format date mm dd yyyy' 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
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}
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

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}
8static formatDateYYYYMMDD(date) {
9 return DateFormatter.format(date, "yyyy-MM-dd");
10}
234function formatDate(date) {
235 var hours = date.getHours();
236 var minutes = date.getMinutes();
237 var ampm = hours >= 12 ? 'pm' : 'am';
238 hours = hours % 12;
239 hours = hours ? hours : 12; // the hour '0' should be '12'
240 minutes = minutes < 10 ? '0'+minutes : minutes;
241 var strTime = hours + ':' + minutes + ' ' + ampm;
242 return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
243}
7function formatDate (date) {
8 const monthNames = [
9 'January', 'February', 'March',
10 'April', 'May', 'June', 'July',
11 'August', 'September', 'October',
12 'November', 'December'
13 ]
14
15 let day = date.getDate()
16 day = day.toString().length > 1 ? day : '0' + day
17 const monthIndex = date.getMonth()
18 const year = date.getFullYear()
19
20 return day + ' ' + monthNames[monthIndex] + ' ' + year + ' — '
21}
486formatDate(d) {
487 const calendar = this;
488 const date = new Date(d);
489 const year = date.getFullYear();
490 const month = date.getMonth();
491 const month1 = month + 1;
492 const day = date.getDate();
493 const weekDay = date.getDay();
494 const { monthNames, monthNamesShort, dayNames, dayNamesShort } = calendar;
495 const { dateFormat, locale } = calendar.params;
496 if (typeof dateFormat === 'string') {
497 return dateFormat
498 .replace(/yyyy/g, year)
499 .replace(/yy/g, String(year).substring(2))
500 .replace(/mm/g, month1 < 10 ? `0${month1}` : month1)
501 .replace(/m(\W+)/g, `${month1}$1`)
502 .replace(/MM/g, monthNames[month])
503 .replace(/M(\W+)/g, `${monthNamesShort[month]}$1`)
504 .replace(/dd/g, day < 10 ? `0${day}` : day)
505 .replace(/d(\W+)/g, `${day}$1`)
506 .replace(/DD/g, dayNames[weekDay])
507 .replace(/D(\W+)/g, `${dayNamesShort[weekDay]}$1`);
508 }
509 if (typeof dateFormat === 'function') {
510 return dateFormat(date);
511 }
512 // Intl Object
513 const formatter = new Intl.DateTimeFormat(locale, dateFormat);
514 return formatter.format(date);
515}
11function formatDate(date, fmt) { //author: meizz 
12 var o = {
13 "M+": date.getMonth() + 1//月份 
14 "d+": date.getDate() //日 
15 };
16
17 if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
18 for (var k in o) {
19 if (new RegExp("(" + k + ")").test(fmt))
20 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
21 }
22
23 return fmt;
24}
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}
32function formatDate(date) {
33 return weekdays[date.getDay()] + ", " +
34 date.getDate() + nth(date.getDate()) + " " +
35 months[date.getMonth()] + " " +
36 date.getFullYear();
37}
5var formatDate = function formatDate(date) {
6 var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'yyyy-MM-dd hh:mm:ss';
7
8 if (!date) return;
9 var Y = date.getFullYear();
10 var M = date.getMonth() + 1;
11 var D = date.getDate();
12 var h = date.getHours();
13 var m = date.getMinutes();
14 var s = date.getSeconds();
15
16 var rules = {
17 yyyy: Y,
18 M: M,
19 MM: formatNumber(M),
20 d: D,
21 dd: formatNumber(D),
22 h: h,
23 hh: formatNumber(h),
24 m: m,
25 mm: formatNumber(m),
26 s: s,
27 ss: formatNumber(s)
28 };
29 var arr = format.split(/-| |:|\//);
30 var formatDate = format;
31 for (var i = 0; i < arr.length; i++) {
32 var el = arr[i];
33 formatDate = formatDate.replace(el, rules[el]);
34 }
35 return formatDate;
36};

Related snippets