使用 Moment.js 的 UTC 时间格式
Time formatting from UTC with Moment.js
如何使用 moment.js
将 utc 时间转换为这种格式
"YYYY Do MM hh:mm:ss"
我正在尝试遵循并使用以下教程的 "Parse a date" 部分中的方法:https://flaviocopes.com/momentjs/
moment(dt).format("YYYY Do MM hh:mm:ss");
但这只会让日期消失。
代码笔:https://codepen.io/centem/pen/ExYEoLj
var options = {
valueNames: ['date', 'number']
};
var ResultDiv = new List('ResultDiv', options);
ul {
list-style-type: none;
padding-left: 0;
margin-left: 0;
}
span {
margin-right: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<div id="ResultDiv">
<input class="search" placeholder="Search" />
<button class="sort" data-sort="date">
Sort by Date
</button>
<button class="sort" data-sort="number">
Sort by Number
</button>
<button>Last 7 Days</button>
<button>Last 30 Days</button>
<ul class="list" id="ul">
<li>
<span class="2019-01">
<script>
let dt = new Date("2019-04-15T15:10:45Z");
document.write(dt);
//2019-04-15T15:10:45Z;
//moment(dt).format("YYYY Do MM hh:mm:ss");
</script>
</span>
<span class="number">76</span>
</li>
<li>
<span class="date">2019-02-12T11:21:10Z</span>
<span class="number">23</span>
</li>
<li>
<span class="date">2019-01-12T10:20:16Z</span>
<span class="number">14</span>
</li>
<li>
<span class="date">2019-01-09T09:20:16Z</span>
<span class="number">7</span>
</li>
<li>
<span class="date">2019-07-11T11:01:05Z</span>
<span class="number">101</span>
</li>
<li>
<span class="date">2019-08-29T11:01:05Z</span>
<span class="number">121</span>
</li>
<li>
<span class="date">2019-09-11T11:01:05Z</span>
<span class="number">130</span>
</li>
</ul>
</div>
解决方案:
let current_datetime = new Date("2019-04-15T15:10:45Z")
let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds()
console.log(formatted_date)
let dt = new Date("2019-04-15T15:10:45Z");
document.write(formatted_date);
您的问题是调用 <script>
标签时未加载 momentjs
。 CodePen 在 DOM 加载后加载 JavaScript。您需要将代码移动到 CodePen 的 JavaScript 部分。我建议将日期添加到您的节点并查询这些节点,通过 innerText 更新节点的内容。我已经更新了你的笔:https://codepen.io/kkirby/pen/zYOWJxX
如何使用 moment.js
将 utc 时间转换为这种格式"YYYY Do MM hh:mm:ss"
我正在尝试遵循并使用以下教程的 "Parse a date" 部分中的方法:https://flaviocopes.com/momentjs/
moment(dt).format("YYYY Do MM hh:mm:ss");
但这只会让日期消失。
代码笔:https://codepen.io/centem/pen/ExYEoLj
var options = {
valueNames: ['date', 'number']
};
var ResultDiv = new List('ResultDiv', options);
ul {
list-style-type: none;
padding-left: 0;
margin-left: 0;
}
span {
margin-right: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<div id="ResultDiv">
<input class="search" placeholder="Search" />
<button class="sort" data-sort="date">
Sort by Date
</button>
<button class="sort" data-sort="number">
Sort by Number
</button>
<button>Last 7 Days</button>
<button>Last 30 Days</button>
<ul class="list" id="ul">
<li>
<span class="2019-01">
<script>
let dt = new Date("2019-04-15T15:10:45Z");
document.write(dt);
//2019-04-15T15:10:45Z;
//moment(dt).format("YYYY Do MM hh:mm:ss");
</script>
</span>
<span class="number">76</span>
</li>
<li>
<span class="date">2019-02-12T11:21:10Z</span>
<span class="number">23</span>
</li>
<li>
<span class="date">2019-01-12T10:20:16Z</span>
<span class="number">14</span>
</li>
<li>
<span class="date">2019-01-09T09:20:16Z</span>
<span class="number">7</span>
</li>
<li>
<span class="date">2019-07-11T11:01:05Z</span>
<span class="number">101</span>
</li>
<li>
<span class="date">2019-08-29T11:01:05Z</span>
<span class="number">121</span>
</li>
<li>
<span class="date">2019-09-11T11:01:05Z</span>
<span class="number">130</span>
</li>
</ul>
</div>
解决方案:
let current_datetime = new Date("2019-04-15T15:10:45Z")
let formatted_date = current_datetime.getFullYear() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getDate() + " " + current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds()
console.log(formatted_date)
let dt = new Date("2019-04-15T15:10:45Z");
document.write(formatted_date);
您的问题是调用 <script>
标签时未加载 momentjs
。 CodePen 在 DOM 加载后加载 JavaScript。您需要将代码移动到 CodePen 的 JavaScript 部分。我建议将日期添加到您的节点并查询这些节点,通过 innerText 更新节点的内容。我已经更新了你的笔:https://codepen.io/kkirby/pen/zYOWJxX