javascript - How to format the current date format in Vue.js - Stack Overflow

I need to get the current date in Vue.js. For that, I used the following method.today_date: new Date().

I need to get the current date in Vue.js.

For that, I used the following method.

today_date: new Date().toJSON().slice(0,10).replace(/-/g,'.')

today_date will give the date as 2019.09.11 format.

Is there any method to customize this format? Actually I need to get the date as 11.09.2019 format. But it's better to know whether there are solutions to get the date in several formats.

I need to get the current date in Vue.js.

For that, I used the following method.

today_date: new Date().toJSON().slice(0,10).replace(/-/g,'.')

today_date will give the date as 2019.09.11 format.

Is there any method to customize this format? Actually I need to get the date as 11.09.2019 format. But it's better to know whether there are solutions to get the date in several formats.

Share Improve this question edited Jul 23, 2020 at 22:25 Boussadjra Brahim 1 asked Sep 11, 2019 at 9:30 Gayan S. MuthukumaranaGayan S. Muthukumarana 9041 gold badge11 silver badges29 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

In pure Javascript you should hard code the format that you want by getting the day, month and year, and concatenate them to get what you want, but i remend to use the moment.js library to format the date easily like :

moment().format('MMMM Do YYYY, h:mm:ss a'); // September 11th 2019, 10:52:10 am
moment().format('dddd');                    // Wednesday
moment().format("MMM Do YY");               // Sep 11th 19
moment().format('YYYY [escaped] YYYY');    

Can be done in different ways One brilliant lib: MomentJS (which can take care of a lot of formats, locales and operations too), but one solution using pure JS could be:

function dtFormatter(d) {
  const yr = d.getFullYear();
  const mnt =
    d.getMonth() + 1 < 9 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
  const day = d.getDate() < 9 ? "0" + d.getDate() : d.getDate();

  return day + "." + mnt + "." + yr;
}
console.log(dtFormatter(new Date()));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744063900a4552286.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信