[英]Format Date to human readable within function
我有一個函數,我正在嘗試格式化日期和時間或目前僅格式化日期。
function(){
var d = new Date();
var n = d.getTime();
return 'VZW Dfill - ' + n;}
我試過的
function(){
var d = new Date();
return 'VZW Dfill - ' + d;}
這返回
VZW Dfill - Thu Jan 30 2020 103924 GMT-0500 (Eastern Standard Time)
我希望格式為 2020JAN30
我也嘗試了以下方法,但這不起作用
function(){
var d = new Date('YYYYMDD');
return 'VZW Dfill - ' + d;}
以上破壞了功能。
任何幫助表示贊賞。
使用純 JavaScript 這實際上非常復雜。 這是(許多)解決方案之一:
var now = new Date();
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
var formattedDate = now.getFullYear() + months[now.getMonth()] + now.getDate();
alert(formattedDate);
使用上面的代碼,編寫以下函數:
function(){
var d = new Date();
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
d = d.getFullYear() + months[d.getMonth()] + d.getDate();
return 'VZW Dfill - ' + d;}
有一個關於JavaScript格式的日期相當廣泛的線程在這里。 其中大多數涉及(常見)第三方軟件包。
您也可以使用語言環境
console.log(today.toLocaleDateString("zh-TW")); // 2020/1/30
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.