Javascript 如何在javascript中以dd/mm/yy格式格式化json日期?

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

How can I format a json date in dd/mm/yy format in javascript?

javascriptjsondatetimedatetime-format

提问by andy

I have a json date like \/Date(1334514600000)\/in my response and when I convert it in javascript then I got this date Tue Apr 17 2012 11:37:10 GMT+0530 (India Standard Time), but I need the date format like 17/04/2012and I fail every time. Can anyone tell me how can I resolve it?

我的\/Date(1334514600000)\/响应中有一个 json 日期,当我在 javascript 中转换它时,我得到了这个日期Tue Apr 17 2012 11:37:10 GMT+0530 (India Standard Time),但我需要类似的日期格式,但17/04/2012每次都失败。谁能告诉我我该如何解决?

回答by row1

I don't think that the other posted answers are quite right, you have already accepted one as working for you so I won't edit it.

我认为其他发布的答案不太正确,您已经接受了一个为您工作,因此我不会对其进行编辑。

Here is an updated version of your accepted answer.

这是您接受的答案的更新版本。

var dateString = "\/Date(1334514600000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);

It uses a technique from this answerto extract the epoch from the JSON date.

它使用此答案中的技术从JSON 日期中提取纪元。

回答by silvajnr

I found very helpful the row1 answer, however i got stuck on the format for input type="date" as only returns one string for decimals under 10, I was able to modify to work on input type="date", I basically adapted the code from row1 to the code from the link http://venkatbaggu.com/convert-json-date-to-date-format-in-jquery/

我发现 row1 的答案非常有用,但是我被困在输入类型 =“日期”的格式上,因为只返回一个小于 10 的小数字符串,我能够修改以处理输入类型 =“日期”,我基本上适应了从 row1 到链接http://venkatbaggu.com/convert-json-date-to-date-format-in-jquery/的代码

I was able through jquery .val add the date to the input

我能够通过 jquery .val 将日期添加到输入中

var dateString = "\/Date(1334514600000)\/".substr(6);
var currentTime = new Date(parseInt(dateString));
var month = ("0" + (currentTime.getMonth() + 1)).slice(-2);
var day = ("0" + currentTime.getDate()).slice(-2);
var year = currentTime.getFullYear();
var date = year + '-' + month + '-' + day;
alert(date);

回答by ujval

//parse JSON formatted date to javascript date object
var bdate = new Date(parseInt(emp.Birthdate.substr(6)));

//format display date (e.g. 04/10/2012)
var displayDate = $.datepicker.formatDate("mm/dd/yy", bdate);

回答by krishnakant alle

Easiest way of formatting date is by using pipes if you are using Angular. Click here

如果您使用的是 Angular,则格式化日期的最简单方法是使用管道。点击这里

//in .ts file
ngOnInit() {
 this.currentDate = new Date()
}
//in html file
<p>Current date is:</p>{{currentDate | date: 'dd/MM/yyyy'}}

//Output: 22/04/2020

回答by Elvis D'Souza

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = day + "/" + month + "/" + year
alert(date);

回答by Mr_DeLeTeD

It's answer to your question...

这是对你问题的回答...

Build the date object with your timestamp

使用您的时间戳构建日期对象

var currentTime = new Date(1334514600000)
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var date = day + "/" + month + "/" + year
alert(date);?

it works

有用

http://jsfiddle.net/ChgUa/

http://jsfiddle.net/ChgUa/