Skip to content

Instantly share code, notes, and snippets.

Created February 24, 2021 20:14
What would you like to do?
format local date with JSON format
function toJSONLocal (date = new Date()) {
var local = new Date(date);
local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return local.toJSON().slice(0, 10);
Copy link

Rplus commented Feb 24, 2021

function toJSONLocal (date = new Date()) {
  return new Date(date).toLocaleDateString('zh', {
    day : '2-digit',
    month : '2-digit',
    year : 'numeric',
  }).replace(/\//g, '-');

toLocaleDateString with option support: IE11+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment