简体   繁体   English

翻译时间戳出生日期 - 以当天的名义

[英]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.我正在尝试以当天的“名称”翻译出生日期,例如星期一,星期二等,但我对如何做到这一点有些疑问,我首先想:获取两个时间戳(出生日期和当前日期时间戳),然后使用 %7 之类的“模”,然后使用模的“其余”查看名称数组。 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.您可以使用valueOf() function 获取 UNIX 时间戳,您可以在其中使用模数,但您可以尝试使用更简单的 API 从日期获取日期名称。 I have taken the actual date of birth, say 14 April 1983 in a timestamp format.我以时间戳格式记录了实际的出生日期,例如 1983 年 4 月 14 日。 I get the monthly date value and month value form the actual DOB.我从实际 DOB 中获得每月日期值和月份值。 I construct another date object with the monthly-date and month value and current year's value.我用每月日期和月份值以及当年值构造另一个日期 object。 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.然后我从该日期获取每周日值(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() + ")");

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.如果您有一个真实的Date object,您可以将它的getDay()方法与工作日数组结合使用。 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 返回格式化的实际生日、原始出生日期和今年生日的日期:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM