我注意到JavaScript的new Date()功能在接受多种格式的日期时非常聪明.
Xmas95 = new Date("25 Dec, 1995 23:15:00")
Xmas95 = new Date("2009 06 12,12:52:39")
Xmas95 = new Date("20 09 2006,12:52:39")
Run Code Online (Sandbox Code Playgroud)
在调用new Date()函数时,我无法在任何地方找到显示所有有效字符串格式的文档.
这用于将字符串转换为日期.如果我们看一下相反的方面,即将日期对象转换为字符串,直到现在我的印象是JavaScript没有内置的API来将日期对象格式化为字符串.
编者注:以下方法是提问者的企图,关于特定浏览器的工作,但也不会在一般的工作; 请参阅此页面上的答案以查看一些实际解决方案.
今天,我在toString()日期对象上使用了该方法,并且令人惊讶的是它用于将日期格式化为字符串.
var d1 = new Date();
d1.toString('yyyy-MM-dd'); //Returns "2009-06-29" in Internet Explorer, but not Firefox or Chrome
d1.toString('dddd, MMMM ,yyyy') //Returns "Monday, June 29,2009" in Internet Explorer, but not Firefox or Chrome
Run Code Online (Sandbox Code Playgroud)
在这里,我找不到任何关于我们可以将日期对象格式化为字符串的方法的文档.
列出Date()对象支持的格式说明符的文档在哪里?
我的表基于Vue.js网站上的网格组件示例
我在表中排序日期时遇到问题.我从服务器端获取所有表数据作为JSON.所以在提供的代码中,我只是模拟了var mockDataFromServerSide中的数据.
这是代码:https://jsfiddle.net/5w1wzhvw/3/
HTML文件:
<!-- component template -->
<script type="text/x-template" id="grid-template">
<table>
<thead>
<tr>
<th v-for="key in columns"
v-on:click="sortBy(key)"
:class="{active: sortKey == key}">
{{key | capitalize}}
<span class="arrow"
:class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
</span>
</th>
</tr>
</thead>
<tbody>
<tr v-for="
entry in data
| filterBy filterKey
| orderBy sortKey sortOrders[sortKey]">
<td v-for="key in columns">
{{entry[key]}}
</td>
</tr>
</tbody>
</table>
</script>
<!-- demo root element -->
<div id="demo">
<form id="search"> …Run Code Online (Sandbox Code Playgroud) date ×2
javascript ×2
date-format ×1
datetime ×1
html-table ×1
sorting ×1
time-format ×1
vue.js ×1