开发者

translate timestamp date of birth - in the name of the day

i'm trying to开发者_运维问答 translate a birth date in the "name" of the day, like monday, tuesday, etc. but i have some doubts on how to do it, i thought first : take the two timestamps (date of birth and current timestamp) and then use a "modulo" like %7 , then with the "rest" of the modulo looking through an array of names. But, actually, the timestamp is not meant to be divided by a modulo isn't it? how would you do?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜