Javascript 在javascript中格式化日期字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44493088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:34:11  来源:igfitidea点击:

Format a date string in javascript

javascriptstringcustomization

提问by user7334203

Hello every i have date field of type string with iso format like this: const date = "2017-06-10T16:08:00: i want somehow to edit the string in the following format like this: 10-06-2017 but i'm struggling in achieving this. I cut the substring after the "T" character

你好,每个我都有这样的 iso 格式的字符串类型的日期字段: const date = "2017-06-10T16:08:00: 我想以某种方式以如下格式编辑字符串:10-06-2017 但我'正在努力实现这一目标。我在“T”字符之后剪切了子字符串

回答by daan.desmedt

Use Moment.jsand the .formatfunction.

使用Moment.js.format函数。

moment('2017-06-10T16:08:00').format('MM/DD/YYYY');

Will output

会输出

06/10/2017

Beside the formatfunction Moment.jswill enrich you will alot more useful functions.

除了format功能之外,Moment.js还会丰富你更多有用的功能。

回答by Ivan Mladenov

It can be achieved without moment.js, but I suggest you use it

不用moment.js也可以实现,但是我建议你使用它

var date = new Date("2017-06-10T16:08:00");

var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();

if (day < 10) {
  day = '0' + day;
}
if (month < 10) {
  month = '0' + month;
}

var formattedDate = day + '-' + month + '-' + year

回答by micebrain

If the date string is always in ISO format, you can also use regex to reformat without other library:

如果日期字符串始终为 ISO 格式,您还可以使用正则表达式重新格式化,而无需其他库:

date.replace(/(\d{4})\-(\d{2})\-(\d{2}).*/, '--')

回答by Viplock

I would like to suggest to use moment js find it - http://momentjs.com/docs/

我想建议使用moment js找到它 - http://momentjs.com/docs/

and use it like

并像使用它一样

    moment(date.toString()).format("MM/DD/YYYY")

回答by ArthurKay

You can use the JavaScript date() built in function to get parts of the date/time you want. For example to display the time is 10:30:

您可以使用 JavaScript date() 内置函数来获取您想要的部分日期/时间。例如显示时间是10:30:

<script>
var date = new Date();
 var min = date.getMinutes();
  var hour = date.getHour();
   document.write(hour+":"+min);
   </script>

To get the year, month, date, day of week use

获取年、月、日、星期几使用

  • getFullYear();

  • getMonth();

  • getDate();

  • getDay();

  • getFullYear();

  • 获取月份();

  • 获取日期();

  • getDay();

To get the date you posted:

要获取您发布的日期:

回答by monikapatelIT

Using Date.toJSON()

使用 Date.toJSON()

function formatDate(userDate) {
  // format from M/D/YYYY to YYYYMMDD
  return (new Date(userDate).toJSON().slice(0,10).split('-').reverse().join('-'));
}

console.log(formatDate("2017-06-10T16:08:00"));

回答by SkipHyman

If you're looking to do this in vanilla javascript, @Ivan Mladenov's answer is great and can be consolidated slightly using padStart.

如果您想在 vanilla javascript 中执行此操作,@Ivan Mladenov 的回答很棒,可以使用padStart稍微巩固一下

const date = new Date()
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')

console.log(`${day}-${month}-${year}`)