Javascript 如何在javascript中将完整日期转换为短日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2035699/
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-22 22:30:01  来源:igfitidea点击:

How to convert a full date to a short date in javascript?

javascriptdateformatting

提问by chobo2

I have a date like this Monday, January 9, 2010

我有一个这样的约会,2010 年 1 月 9 日星期一

I now want to convert it to

我现在想将其转换为

1/9/2010 mm/dd/yyyy

1/9/2010 毫米/日/年

I tried to do this

我试图这样做

    var startDate = "Monday, January 9, 2010";
    var convertedStartDate = new Date(startDate);
    var month = convertedStartDate.getMonth() + 1
    var day = convertedStartDate.getDay();
    var year = convertedStartDate.getFullYear();
    var shortStartDate = month + "/" + day + "/" + year;

However it must be thinking the date is in a different format since day returns 1 instead of 9.

但是,它必须认为日期采用不同的格式,因为 day 返回 1 而不是 9。

回答by Roland Bouman

The getDay()method returns a number to indicate the day in week (0=Sun, 1=Mon, ... 6=Sat). Use getDate()to return a number for the day in month:

getDay()方法返回一个数字来表示星期几(0=Sun,1=Mon,... 6=Sat)。使用getDate()返回某个数字的一天在一个月:

var day = convertedStartDate.getDate();

If you like, you can try to add a custom format function to the prototype of the Dateobject:

如果你喜欢,你可以尝试在Date对象的原型中添加自定义格式函数:

Date.prototype.formatMMDDYYYY = function(){
    return (this.getMonth() + 1) + 
    "/" +  this.getDate() +
    "/" +  this.getFullYear();
}

After doing this, you can call formatMMDDYYY()on anyinstance of the Dateobject. Of course, this is just a very specific example, and if you really need it, you can write a generic formatting function that would do this based on a formatting string, kinda like java's SimpleDateeFormat (http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html)

这样做后,你可以调用formatMMDDYYY()任何的实例Date对象。当然,这只是一个非常具体的例子,如果你真的需要它,你可以编写一个通用的格式化函数,它会根据格式化字符串来做这件事,有点像 java 的 SimpleDateeFormat ( http://java.sun.com/ j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html)

(tangent: the Dateobject always confuses me... getYear()vs getFullYear(), getDate()vs getDay(), getDate()ranges from 1..31, but getMonth()from 0..11

(切线:Date对象总是让我感到困惑...... getYear()vs getFullYear()getDate()vs getDay()getDate()范围从 1..31,但从getMonth()0..11

It's a mess, and I always need to take a peek. http://www.w3schools.com/jsref/jsref_obj_date.asp)

一团糟,我总是需要偷看一眼。http://www.w3schools.com/jsref/jsref_obj_date.asp)

回答by gnath

Here you go:

干得好:

(new Date()).toLocaleDateString('en-US');

That's it !!

就是这样 !!

you can use it on any date object

您可以在任何日期对象上使用它

let's say.. you have an object called "currentDate"

比方说..你有一个名为“currentDate”的对象

var currentDate = new Date(); //use your date here
currentDate.toLocaleDateString('en-US'); // "en-US" gives date in US Format - mm/dd/yy

(or)

(或者)

If you want it in local format then

如果你想要本地格式,那么

currentDate.toLocaleDateString(); // gives date in local Format

回答by Constantin

Built-in toLocaleDateString()does the job, but it will remove the leading 0s for the day and month, so we will get something like "1/9/1970", which is not perfect in my opinion. To get a proper format MM/DD/YYYYwe can use something like:

内置可以toLocaleDateString()完成这项工作,但它会删除日期和月份的前导 0,所以我们会得到类似“1/9/1970”的东西,在我看来这并不完美。要获得正确的格式,MM/DD/YYYY我们可以使用以下内容:

new Date(dateString).toLocaleDateString('en-US', {
  day: '2-digit',
  month: '2-digit',
  year: 'numeric',
})

回答by Abilash Raghu

var d = new Date("Wed Mar 25 2015 05:30:00 GMT+0530 (India Standard Time)"); 
document.getElementById("demo").innerHTML = d.toLocaleDateString();

回答by TrevorBrooks

date.toLocaleDateString('en-US')works great. Here's some more information on it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

date.toLocaleDateString('en-US')效果很好。这里有一些关于它的更多信息:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

回答by Kodie Grantham

You could do this pretty easily with my date-shortcodepackage:

你可以用我的date-shortcode包很容易地做到这一点:

const dateShortcode = require('date-shortcode')

var startDate = 'Monday, January 9, 2010'
dateShortcode.parse('{M/D/YYYY}', startDate)
//=> '1/9/2010'

回答by a.boussema

I was able to do that with :

我能够做到这一点:

var dateTest = new Date("04/04/2013");
dateTest.toLocaleString().substring(0,dateTest.toLocaleString().indexOf(' '))

the 04/04/2013 is just for testing, replace with your Date Object.

04/04/2013 仅用于测试,请替换为您的日期对象。

回答by user838708

Try this:

尝试这个:

new Date().toLocaleFormat("%x");