[英]How to convert date format into different formate
Hello I want to convert 你好我想转换
March 2018 to 032018
in jQuery.I used 在jQuery.I用
var d = new Date($('.selected_month').find("td:first").text());
But it is giving result is: 但是它给出的结果是:
Thu Mar 01 2018 00:00:00 GMT+0530 (IST)
You need to use getMonth()
and getFullYear()
on returned date object to format date as per requirement. 您需要在返回的日期对象上使用
getMonth()
和getFullYear()
来按照要求格式化日期。 Also, you need to add 1 to returned month as getMonth method is 0 index based: 另外,您需要在返回的月份中加上1,因为getMonth方法基于0索引:
(d.getMonth()+1).toString() + d.getFullYear().toString()
Try this 尝试这个
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
return [month,year].join('');
}
Call this function : formatDate('March 2018') // output : 032018
调用此函数:
formatDate('March 2018') // output : 032018
Try converting the date object you end up with to a string: Try the following snippet, just change var d = new Date()
to var d = new Date($('.selected_month').find("td:first").text())
. 尝试将最终的日期对象转换为字符串:尝试以下代码段,只需将
var d = new Date()
更改为var d = new Date($('.selected_month').find("td:first").text())
。
var d = new Date(); var twoDigitMonth = (d.getUTCMonth() + 1).toString().padStart(2, "0"); var year = d.getUTCFullYear(); var result = twoDigitMonth + year; console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
...or try this... ...或者尝试一下...
console.log( new Date("March 2018") .toLocaleDateString('en-EN', {month: '2-digit',year: 'numeric'}) .replace('/','') )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.