![](/img/trans.png)
[英]How to return the correct day of the week given a date - year month and day
[英]How to get the day of week and the month of the year?
Javascript我了解的不多,查到的其他問題都是關於日期的操作,不是按需獲取信息。
我希望獲得格式如下的日期:
打印於 2011 年 1 月 27 日星期四 17:42:21
到目前為止,我得到以下信息:
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
var prnDt = "Printed on Thursday, " + now.getDate() + " January " + now.getFullYear() + " at " + h + ":" + m + ":" s;
我現在需要知道如何獲取星期幾和一年中的月份(他們的名字)。
有沒有一種簡單的方法來實現它,或者我應該考慮使用 arrays ,我會使用now.getMonth()
和now.getDay()
簡單地索引到正確的值?
是的,你需要數組。
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var day = days[ now.getDay() ];
var month = months[ now.getMonth() ];
或者您可以使用date.js庫。
編輯:
如果您要經常使用這些,您可能需要擴展Date.prototype
以實現可訪問性。
(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
Date.prototype.getMonthName = function() {
return months[ this.getMonth() ];
};
Date.prototype.getDayName = function() {
return days[ this.getDay() ];
};
})();
var now = new Date();
var day = now.getDayName();
var month = now.getMonthName();
使用標准的 javascript Date 類。 不需要數組。 不需要額外的庫。
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }; var prnDt = 'Printed on ' + new Date().toLocaleTimeString('en-us', options); console.log(prnDt);
您還可以做的一件事是擴展日期對象以通過以下方式返回工作日:
Date.prototype.getWeekDay = function() {
var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return weekday[this.getDay()];
}
所以,你只能調用 date.getWeekDay();
var GetWeekDays = function (format) {
var weekDays = {};
var curDate = new Date();
for (var i = 0; i < 7; ++i) {
weekDays[curDate.getDay()] = curDate.toLocaleDateString('ru-RU', {
weekday: format ? format : 'short'
});
curDate.setDate(curDate.getDate() + 1);
}
return weekDays;
};
me.GetMonthNames = function (format) {
var monthNames = {};
var curDate = new Date();
for (var i = 0; i < 12; ++i) {
monthNames[curDate.getMonth()] = curDate.toLocaleDateString('ru-RU', {
month: format ? format : 'long'
});
curDate.setMonth(curDate.getMonth() + 1);
}
return monthNames;
};
不幸的是,javascript 中的Date
對象僅以數字格式返回有關月份的信息。 您可以做的更快的事情是創建一個月份數組(它們不應該經常更改!)並創建一個基於數字返回名稱的函數。
像這樣的東西:
function getMonthNameByMonthNumber(mm) {
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
return months[mm];
}
因此,您的代碼變為:
var prnDt = "Printed on Thursday, " + now.getDate() + " " + getMonthNameByMonthNumber(now.getMonth) + " "+ now.getFullYear() + " at " + h + ":" + m + ":" s;
toLocaleDateString()方法返回一個字符串,其中包含該日期的日期部分的語言敏感表示。 新的語言環境和選項參數讓應用程序指定應使用其格式約定的語言,並允許自定義函數的行為。 在忽略語言環境和選項參數的舊實現中,使用的語言環境和返回字符串的形式完全取決於實現。
長格式:
const options = { weekday: 'long' };
const date = new Date();
console.log(date.toLocaleDateString('en-US', options));
一個班輪:
console.log(new Date().toLocaleDateString('en-US', { weekday: 'long' }));
注意:區域設置還有其他語言選項,但此處針對美國英語提供的選項
使用http://phrogz.net/JS/FormatDateTime_JS.txt你可以:
var now = new Date;
var prnDt = now.customFormat( "Printed on #DDDD#, #D# #MMMM# #YYYY# at #hhh#:#mm#:#ss#" );
這很簡單。 您可以在 toLocaleDateString() 中設置選項以僅顯示工作日以獲取名稱。 例如:
(new Date()).toLocaleDateString('en-US',{ weekday: 'long'}) 將只返回星期幾。 並且 (new Date()).toLocaleDateString('en-US',{ month: 'long'}) 將僅返回一年中的月份。
function currentDate() {
var monthNames = [ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ];
var days = ['SUNDAY','MONDAY','TUESDAY','WEDNESDAY','THURSDAY','FRIDAY','SATURDAY'];
var today = new Date();
var dd = today.getDate();
var mm = monthNames[today.getMonth()];
var yyyy = today.getFullYear();
var day = days[today.getDay()];
today = 'Date is :' + dd + '-' + mm + '-' + yyyy;
document.write(today +"<br>");
document.write('Day is : ' + day );
}
currentDate();
我認為這是我找到的最佳解決方案
new Date(payload.value).toLocaleString("en", { weekday: "long" })
如果一周從 1 開始,我們可以使用以下代碼段:'Monday',2:'Tuesday', 3:'Wednesday',4:'Thursday', 5:'Friday',6:'Saturday',7:'星期天'。
let weekDays = {
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
7:'Sunday'};
return weekDays[dt];
new Date().toUTCString()
可能足以滿足您的目的。 它將字符串格式化為: Tue, 31 Dec 2024 23:59:59 GMT
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString
寫入 Date = <script> document.write(Date()); </script>
<script> document.write(Date()); </script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.