Thanks
You can get the UNIX Timestamp using valueOf()
function where you can use modulo but you might try using easier API to get the day name from a date. I have taken the actual date of birth, say 14 April 1983 in a timestamp format. I get the monthly date value and month value form the actual DOB. I construct another date object with the monthly-date and month value and current year's value. Then I get the weekly day value (0-6 = Sun-Sat) from this date, and show the mapped day name from the array containing the names of the days.
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() + ")");
Hope this helps.
If you have a real Date
object, you can use the getDay()
method of it in combination with an array of weekdays. Same goes for months. Here's a function to return the formatted actual birthday, the original day of birth and the day for the birthday this year:
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
精彩评论