admin管理员组文章数量:1326345
ASP.NET MVC 5 .Net 4.6.1 c#
I set a date on the server like so:
public JsonResult GetDate()
{
var date = DateTime.Now;
return Json(date);
}
When I call this method via ajax, the date is returned like so:
Date(1464670800000)
I know I can format my date at the server, but i dont want to do that because the date format changes for different sections of the page, so i want to format the date client side. How do I convert that date object returned from the server to a formatted string (mm/dd/yy for instance) in javascript? Thanks
ASP.NET MVC 5 .Net 4.6.1 c#
I set a date on the server like so:
public JsonResult GetDate()
{
var date = DateTime.Now;
return Json(date);
}
When I call this method via ajax, the date is returned like so:
Date(1464670800000)
I know I can format my date at the server, but i dont want to do that because the date format changes for different sections of the page, so i want to format the date client side. How do I convert that date object returned from the server to a formatted string (mm/dd/yy for instance) in javascript? Thanks
Share Improve this question asked Jun 22, 2016 at 22:20 BoundForGloryBoundForGlory 4,42715 gold badges59 silver badges83 bronze badges 5- There's no built-in date-formatting functionality in JavaScript. If it's a simple format you can retrieve the date parts from the date object and convert them to strings and piece them together. For more plicated formatting, use a library like moment.js that has built-in format strings closer to those you know from C#. – Cᴏʀʏ Commented Jun 22, 2016 at 22:22
- @cory - I'm hoping someone knows of an awesome date library that was built in javascript. I'll look at moment. Thanks – BoundForGlory Commented Jun 22, 2016 at 22:25
- Send a valid ISO string to page – charlietfl Commented Jun 22, 2016 at 22:43
- Be aware of time zones. – prashkr Commented Jun 22, 2016 at 23:31
- Luckily, for this app, i will only have 1 time zone. Thanks for the warning anyway – BoundForGlory Commented Jun 22, 2016 at 23:44
2 Answers
Reset to default 4You can do it manually:
function formatDate(timestamp){
var x=new Date(timestamp);
var dd = x.getDate();
var mm = x.getMonth()+1;
var yy = x.getFullYear();
return dd +"/" + mm+"/" + yy;
}
console.log(formatDate(new Date()));
Or you can use moments.js lib.
http://momentjs./
moment(date).format("DD/mm/YY");
I tend to use a regex on these to only get the numbers
replace(/\D/g, ''))
And just pass your Date(1464670800000)
through that and then into the new Date
constructor and work with it from there.
console.log(
new Date(+"Date(1464670800000)".replace(/\D/g, ''))
)
//or for example, use LocaleDate
console.log(
new Date(+"Date(1464670800000)".replace(/\D/g, '')).toLocaleDateString()
)
(The +
is converting the string to an int so that "1464670800000"
just bees 1464670800000
to conform to the Date
constructor)
本文标签: cConvert datetime from server to string javascriptStack Overflow
版权声明:本文标题:c# - Convert datetime from server to string javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742198923a2431596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论