Skip to content

Instantly share code, notes, and snippets.

@Rplus
Created February 24, 2021 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rplus/ddc44e4634e662e7902b5b78a96033c0 to your computer and use it in GitHub Desktop.
Save Rplus/ddc44e4634e662e7902b5b78a96033c0 to your computer and use it in GitHub Desktop.
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