Javascript 以 dd/mm/yyyy hh:mm:ss 格式转换日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42862729/
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
Convert date object in dd/mm/yyyy hh:mm:ss format
提问by Rihana
I have a datetime object and its value is as follows
我有一个日期时间对象,它的值如下
2017-03-16T17:46:53.677
Can someone please let me know how to convert this to dd/mm/yyyy hh:mm:ss format I googled a lott and could not find format conversion for this particular input.
有人可以让我知道如何将其转换为 dd/mm/yyyy hh:mm:ss 格式我在谷歌上搜索了很多,但找不到此特定输入的格式转换。
回答by corn3lius
You can fully format the string as mentioned in other posts. But I think your better off using the locale functions in the date object?
您可以完全格式化其他帖子中提到的字符串。但我认为您最好在日期对象中使用语言环境函数?
var d = new Date("2017-03-16T17:46:53.677");
console.log( d.toLocaleString() );
edit :
编辑 :
ISO 8601( the format you are constructing with ) states the time zone is appended at the end with a [{+|-}hh][:mm]at the end of the string.
ISO 8601(您正在构建的格式)规定时区附加在[{+|-}hh][:mm]字符串末尾的末尾。
so you could do this :
所以你可以这样做:
var tzOffset = "+07:00"
var d = new Date("2017-03-16T17:46:53.677"+ tzOffset);
console.log(d.toLocaleString());
var d = new Date("2017-03-16T17:46:53.677"); // assumes local time.
console.log(d.toLocaleString());
var d = new Date("2017-03-16T17:46:53.677Z"); // UTC time
console.log(d.toLocaleString());
edit :
编辑 :
Just so you know the localefunction displays the date and time in the manner of the users language and location. European date is dd/mm/yyyyand US is mm/dd/yyyy.
只是为了让您知道该locale功能以用户语言和位置的方式显示日期和时间。欧洲日期是dd/mm/yyyy,美国是mm/dd/yyyy。
var d = new Date("2017-03-16T17:46:53.677");
console.log(d.toLocaleString("en-US"));
console.log(d.toLocaleString("en-GB"));
回答by Marco Salerno
Here we go:
开始了:
var today = new Date();
var day = today.getDate() + "";
var month = (today.getMonth() + 1) + "";
var year = today.getFullYear() + "";
var hour = today.getHours() + "";
var minutes = today.getMinutes() + "";
var seconds = today.getSeconds() + "";
day = checkZero(day);
month = checkZero(month);
year = checkZero(year);
hour = checkZero(hour);
mintues = checkZero(minutes);
seconds = checkZero(seconds);
console.log(day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + seconds);
function checkZero(data){
if(data.length == 1){
data = "0" + data;
}
return data;
}
回答by Canolyb1
In vanilla js you can use the .getMonth(), .getYear and .getDate() methods then format the string at you would like.
在 vanilla js 中,您可以使用 .getMonth()、.getYear 和 .getDate() 方法,然后根据需要格式化字符串。
Here is more information:
以下是更多信息:
http://www.webdevelopersnotes.com/10-ways-to-format-time-and-date-using-javascript
http://www.webdevelopersnotes.com/10-ways-to-format-time-and-date-using-javascript

