Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
format local date with JSON format
// https://stackoverflow.com/a/11172083
function toJSONLocal (date = new Date()) {
var local = new Date(date);
local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return local.toJSON().slice(0, 10);
}
@Rplus
Copy link
Author

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+
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

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