Skip to content

Latest commit

 

History

History
23 lines (16 loc) · 518 Bytes

File metadata and controls

23 lines (16 loc) · 518 Bytes

formatDate()

Overview

Formats a Date object into a string based on the specified format.

Code

A screenshot of the titular code snippet

const formatDate = (date, format) => {
  let day = date.getDate();
  let month = date.getMonth() + 1;
  let year = date.getFullYear();

  format = format.replace("dd", day < 10 ? "0" + day : day);
  format = format.replace("MM", month < 10 ? "0" + month : month);
  format = format.replace("yyyy", year);

  return format;
}