[英]translate timestamp date of birth - in the name of the day
我正在嘗試以當天的“名稱”翻譯出生日期,例如星期一,星期二等,但我對如何做到這一點有些疑問,我首先想:獲取兩個時間戳(出生日期和當前日期時間戳),然后使用 %7 之類的“模”,然后使用模的“其余”查看名稱數組。 但是,實際上,時間戳並不是要除以模數,不是嗎? 你會怎么做?
謝謝
您可以使用valueOf()
function 獲取 UNIX 時間戳,您可以在其中使用模數,但您可以嘗試使用更簡單的 API 從日期獲取日期名稱。 我以時間戳格式記錄了實際的出生日期,例如 1983 年 4 月 14 日。 我從實際 DOB 中獲得每月日期值和月份值。 我用每月日期和月份值以及當年值構造另一個日期 object。 然后我從該日期獲取每周日值(0-6 = Sun-Sat),並從包含日期名稱的數組中顯示映射的日期名稱。
var days = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday";
var actualDOB = "04/14/1983";
var date = new Date(new Date().getFullYear(), new Date(actualDOB).getMonth(), new Date(actualDOB).getDate());
alert("The day of the week this year for your birthday is : " + days.split(',')[date.getDay()] + " (" + date.toDateString() + ")");
希望這可以幫助。
如果您有一個真實的Date
object,您可以將它的getDay()
方法與工作日數組結合使用。 幾個月也一樣。 這是一個 function 返回格式化的實際生日、原始出生日期和今年生日的日期:
function birthDAY(dat){
var result = {},
birthday = new Date(dat),
weekdays = 'sun,mon,tue,wedness,thurs,fri,satur'.split(','),
months = 'jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec'.split(','),
dateFormatted = function(dateobj) {
return [
weekdays[dateobj.getDay()],'day',
', ', months[dateobj.getMonth()],
' ', dateobj.getDate(),
' ', dateobj.getFullYear()
].join('');
};
result.bdformatted = dateFormatted(birthday);
result.origbd = weekdays[birthday.getDay()]+'day';
birthday.setFullYear(new Date().getFullYear());
result.bdthisyear = weekdays[birthday.getDay()]+'day');
return result;
}
//usage
var bdObj = birthDAY('1973/11/02'); // <- your timestamp here
alert(bdObj.bdformatted); //=> friday, nov 2 1973
alert(bdObj.origbd); //=> friday
alert(bdObj.bdthisyear); //=> wednessday
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.