简体   繁体   English

格式化javascript对象以不显示其他语言环境信息

[英]Formating a javascript object not to display extra locale information

I am trying to bind data from the database to an html page. 我正在尝试将数据库中的数据绑定到html页面。 I am using ms ajax templates to do this . 我正在使用ms ajax模板来做到这一点。 The problem comes when i try to bind and format a date object.The following is a snippet of my code 当我尝试绑定日期对象并设置其格式时,问题就来了。以下是我的代码片段

 <p class="font1 pad_bot1">
     <a href="" sys:datadesc="{{StartDate}}" sys:dataid="{{ID}}">
         {{new Date(StartDate).toString("fullDate")}}
     </a>

The result of this code on my front end is as follows: 此代码在我的前端的结果如下:

Thu Aug 01 2013 00:00:00 GMT+0200 (South Africa Standard Time)

I want to display the ' Thu Aug 01 2013 ' part only not the other part. 我只想显示“ 2013年8月1日星期四 ”这一部分,而不是其他部分。 I need a solution of how i can format the date without writing a function to do so. 我需要一个解决方案,该如何在不编写函数的情况下格式化日期。 I am assuming javascript can do that out of the box. 我假设javascript可以开箱即用。

I know nothing about ms ajax templates. 我对ms ajax模板一无所知。 But assuming that the Date format behaves the same as in standard javascript, you can format your date manually. 但是,假设Date格式的行为与标准javascript中的相同,则可以手动设置日期格式。 You can find more instructions here . 您可以在此处找到更多说明。

If you have a lot of date formatting to do, you may want to check Moment.js 如果您需要进行大量的日期格式化,则可能需要检查Moment.js。

I found the solution by simply doing this 我只是通过这样做找到了解决方案

{{new Date(StartDate).format('dddd, MMM , yyyy')}} {{new Date(StartDate).format('dddd,MMM,yyyy')}}

I used the format() function instead of toString(). 我使用了format()函数而不是toString()。 Thank you for your contributions 感谢您的贡献

The best option for all international users is to use toLocaleDateString . 对于所有国际用户来说,最好的选择是使用toLocaleDateString

var date = new Date("Thu Aug 01 2013 00:00:00 GMT+0200");
var dateString = date.toLocaleDateString(); 
// dateString will be "7/31/2013" in the US, but customized based on client's location

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString 参考: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM