最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Convert datetime from server to string javascript - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

You 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)

发布评论

评论列表(0)

  1. 暂无评论