我將日期值作爲日期時間從代碼隱藏到我的javascript。我只能在JavaScript中處理它,下面是我目前從代碼隱藏到我的客戶端的字符串。.Net使用javascript的DateTime()到日期
"Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)"
如何將此字符串更改爲僅來自JavaScript的日期。我想要格式爲「dd/mm/yyyy
」。
我將日期值作爲日期時間從代碼隱藏到我的javascript。我只能在JavaScript中處理它,下面是我目前從代碼隱藏到我的客戶端的字符串。.Net使用javascript的DateTime()到日期
"Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)"
如何將此字符串更改爲僅來自JavaScript的日期。我想要格式爲「dd/mm/yyyy
」。
如果你不能改變它的服務器端,然後從這裏開始:
alert(new Date(Date.parse("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)")))
然後根據你自己的答案,你可以在這裏結束了:
function pad(num) {
return ("0"+num).slice(-2);
}
var myCSharpString = "Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)";
var date = new Date(Date.parse(myCSharpString));
alert(pad(date.getUTCDate()) + "/" + pad(date.getMonth() + 1) + "/" + date.getFullYear());
嘗試使用moment.js
var day = moment("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)", "dd/mm/yyyy");
您可以簡單地使用MomentJS的日期。
var format = moment("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)").format("DD-MM-YYYY");
alert(new Date(format));
<script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment.min.js"></script>
確定。
剛剛得到解決方案。
下面是JavaScript代碼以參數作爲.NET的DateTime()字符串
function formatTheDate(strDate) {
if (strDate != '') {
var date = new Date(Date.parse(strDate));
var formatedDate = (date.getUTCDate()) + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
return formatedDate;
}
}
你看呢?有幾百個這些問題在這裏回答 - 從這開始:'new Date(Date.parse(「Mon Oct 05 2015 13:34:29 GMT + 0530(India Standard Time)」))' – mplungjan
@mplungjan yes found解。謝謝你的道路。 –
如果你想要它在'dd/mm/yyyy'中,那麼爲什麼不從服務器端的DateTime對象中特別格式化它,而不是處理你現在使用的異常長的字符串呢? – mason