3 examples of 'mdn date' in JavaScript

Every line of 'mdn date' 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
3export default function formatDA(date, strFormat = 'MMM D, YYYY') {
4 if (!date) {
5 return;
6 }
7
8 try {
9 const parsedDateTime = parse(date, 'yyyyMMdd', new Date());
10 const formattedDateTime = format(parsedDateTime, strFormat);
11
12 return formattedDateTime;
13 } catch (err) {
14 // swallow?
15 }
16}
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

8static formatDateYYYYMMDD(date) {
9 return DateFormatter.format(date, "yyyy-MM-dd");
10}
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}

Related snippets