简体   繁体   English

javascript 格式日期为清晰格式

[英]javascript format date to legible format

I have this code which is getting me the date in a ledgible format but it currently outputs this format 20200819 but I want to convert it to 2020-08-19 is this possible?我有这段代码,它以可记录的格式获取日期,但它目前输出这种格式20200819但我想将其转换为2020-08-19这可能吗?

This is my code这是我的代码

const dateConverter = (dateIn) => {
    var yyyy = dateIn.getFullYear();
    var mm = dateIn.getMonth() + 1; // getMonth() is zero-based
    var dd = dateIn.getDate();
    return String(10000 * yyyy + 100 * mm + dd); // Leading zeros for mm and dd
}
  
var today = new Date();
console.log(dateConverter(today));

You don't need the converter function for that.为此,您不需要转换器 function。 Just use toLocaleDateString with a locale that has this format, like Sweden.只需将toLocaleDateString与具有这种格式的语言环境一起使用,例如瑞典。

To get more certainty about the format, I have added two extensions:为了更加确定格式,我添加了两个扩展:

 console.log(new Date().toLocaleDateString("en-se")); // To be explicit about the format of the numerical parts console.log(new Date().toLocaleDateString("en-se", { year: "numeric", month: "2-digit", day: "2-digit" }) ); // To be explicit about the delimiter also: console.log(new Date().toLocaleDateString("en-se", { year: "numeric", month: "2-digit", day: "2-digit" }).replace(/\D/g, "-") );

Alternative:选择:

If you don't want to rely on the native toLocaleDateString function, then replace the following line in your code:如果您不想依赖本机toLocaleDateString function,请在您的代码中替换以下行:

return String(10000 * yyyy + 100 * mm + dd)

with:和:

return String(10000 * yyyy + 100 * mm + dd).replace(/(....)(..)(..)/,"$1-$2-$3");

You can use padStart to create your own string like below, but I would reccomend looking into the date-fns or moment.js libraries, as they can handle this very nicely.您可以使用padStart创建您自己的字符串,如下所示,但我建议查看 date-fns 或 moment.js 库,因为它们可以很好地处理这个问题。

 const dateConverter = (dateIn) => { var year = dateIn.getFullYear(); var month = dateIn.getMonth() + 1; // getMonth() is zero-based var day = dateIn.getDate(); return year + "-" + month.toString().padStart(2, "0") + "-" + day.toString().padStart(2, "0"); } var today = new Date(); console.log(dateConverter(today));

You could use template literals to get the format you want, im pretty sure their must be a simpler way but, this is as similar to your code as possible您可以使用模板文字来获取所需的格式,我很确定它们一定是一种更简单的方法,但是,这与您的代码尽可能相似

    const dateConverter = (dateIn) => {
  var yyyy = dateIn.getFullYear();
  var mm = dateIn.getMonth() + 1;
  if (mm < 10) mm = `0${mm}`; // getMonth() is zero-based
  var dd = dateIn.getDate();
  if (dd < 10) dd = `0${dd}`;
  return `${yyyy}-${mm}-${dd}`;
};

var today = new Date();
console.log(dateConverter(today));

Output as today: 2020-08-19 Output 今天:2020-08-19

new Date().toISOString().slice(0, 10).split('-').reverse().join('/')




//use following date format methods and options.
var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US

// US English uses month-day-year order
console.log(date.toLocaleDateString('en-US'));
// → "12/19/2012"

// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// → "20/12/2012"

// Korean uses year-month-day order
console.log(date.toLocaleDateString('ko-KR'));
// → "2012. 12. 20."

// Event for Persian, It's hard to manually convert date to Solar Hijri
console.log(date.toLocaleDateString('fa-IR'));
// → "۱۳۹۱/۹/۳۰"

// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleDateString('ar-EG'));
// → "٢٠‏/١٢‏/٢٠١٢"

// for Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
console.log(date.toLocaleDateString('ja-JP-u-ca-japanese'));
// → "24/12/20"

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(date.toLocaleDateString(['ban', 'id']));
// → "20/12/2012"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM