我正在開發一個計算器,它需要根據 3 個輸入來計算出生日期:以年、月和日為單位的年齡:
const arrDatas = [];
const elmDataNasc = document.querySelector('#dataNasc');
function setDataAnos (e) { // store at position 0 of arrDates the years
arrDatas[0] = e;
setDataNasc();
}
function setDataMeses (e) { // store in position 1 of arrDatas the months
arrDatas[1] = e;
setDataNasc();
}
function setDataDias (e) { // store in position 2 of arrDatas the days
arrDatas[2] = e;
setDataNasc();
}
function setDataNasc () {
let anosDias = arrDatas[0] * 365; // convert years to days
let mesesDias = arrDatas[1] * 30.417; // convert months to days (30,417 is the average of days per month)
let diasDias = arrDatas[2]; // Days
let totalDias = anosDias mesesDias diasDias; // add the total number of days and assign it to a variable
let dataData = new Date(); // current date
dataData.setDate( dataData.getDate() - totalDias); // subtract total days from the current date
let data = dataData.toLocaleDateString("pt-BR", { year: "numeric", month: "2-digit", day: "2-digit",}); // format to DD/MM/YYYY
elmDataNasc.value = data; // assign date to birthdate input
}
<form>
<p>Enter data to calculate date of birth:</p>
<label for="years">Years</label>
<input onchange="setDataAnos(this.value)" type="number" name="years" min="0" max="31">
<label for="months">Months</label>
<input onchange="setDataMeses(this.value)" type="number" name="months" min="0" max="11">
<label for="days">Days</label>
<input onchange="setDataDias(this.value)" type="number" name="days" min="0" max="31">
<br>
<br>
<label for="dateBirth">Date of birth:</label>
<input id="dataNasc" type="text" name="dataNasc" value="" disabled>
</form>
該腳本只允許您計算一個大概的日期,因為它沒有考慮每個月或閏年的確切天數。我想讓她計算出準確的出生日期,但我不知道該怎么做,我絞盡腦汁在互聯網上搜索,但一無所獲。有光嗎?
uj5u.com熱心網友回復:
您可以一次設定年、月和日的值,以便 Date 物件為您進行數學運算,包括閏年和不同長度的月份。
考慮一個出生于 1957 年 2 月 28 日的人。在 2020 年 1 月 31 日(基準面)他們是 62 歲 11 個月零 3 天。如果你從資料中減去它,你應該得到正確的生日。
同樣,1957 年 3 月 29 日出生的人在 2020 年 2 月 29 日是 62 歲零 11 個月。
1957 年 3 月 31 日出生的人在 2020 年 2 月 29 日是 62 歲 10 個月零 29 天。
例如
function calcBirthday(y=0, m=0, d=0, datum = new Date()) {
return new Date(
datum.getFullYear() - y,
datum.getMonth() - m,
datum.getDate() - d);
}
// Someone who is 62 years, 11 months and 3 days old on 31 Jan 2020
// was born on 28 Feb 1957
console.log(calcBirthday(62, 11, 3, new Date(2020,0,31))
.toDateString());
// Someone who is 62 years, 11 months old on 29 Feb 2020
// was born on 29 Mar 1957
console.log(calcBirthday(62, 11, 0, new Date(2020,1,29))
.toDateString());
// Someone who is 62 years, 10 months, 29 days old on 29 Feb 2020
// was born on 31 Mar 1957
console.log(calcBirthday(62, 10, 29, new Date(2020,1,29))
.toDateString());
// Someone who is 1 year, 1 month and 1 day old today was
// was born on…
console.log(calcBirthday(1, 1, 1).toDateString());
// Someone who is 1 year old on 29 Feb 2020 was
// was born on…
console.log(calcBirthday(1, 0, 0, new Date(2020,1,29))
.toDateString());
需要注意的是,必須輸入以年、月和日為單位的有效年齡值,否則無法保證結果。2 月 29 日也有一些例外情況。
uj5u.com熱心網友回復:
首先從現在的日期減去天數得到一個新的日期。
然后從該日期減去月數以給出新日期。
然后從該日期減去年數以給出您想要的日期:
const days = 22;
const months = 4;
const years = 63;
let d = new Date();
d.setDate(d.getDate() - days);
d.setMonth(d.getMonth() - months);
d.setFullYear(d.getFullYear() - years);
console.log(d);
這是一個版本,您可以在其中輸入從今天開始的幾天、幾個月和幾年前,并查看當時的日期:
const days =
document.querySelector('#days');
const months =
document.querySelector('#months');
const years =
document.querySelector('#years');
const btn =
document.querySelector('button');
const p =
document.querySelector('p')
//parseInt(quantity, 10)
btn.addEventListener('click',
function(){
const d = parseInt(days.value);
const m = parseInt(months.value);
const y = parseInt(years.value);
const dt = new Date();
dt.setDate(dt.getDate() - d);
dt.setMonth(dt.getMonth() - m);
dt.setFullYear(dt.getFullYear() - y);
p.innerHTML =
`${d} days, ${m} months and ${y} years ago
the date was ${dt.toLocaleDateString()}`;
}
);
<form>
<input id='days' type='number' size='5'>
<label for='days'>days</label>
<br>
<input id='months' type='number' size='5'>
<label for='months'>months</label>
<br>
<input id='years' type='number' size='5'>
<label for='years'>years</label>
<br>
<button type='button'>Submit</button>
</form>
<p></p>
兩個死于不同日子但都活了相同年數、月數和天數的人可能活了不同的總天數。這是因為他們的生活可能跨越了不同數量的 2 月 29 日和結束于 30 日或 31 日的月份。
年、月和日不是衡量時間的好方法,因為它們表示的時間長度取決于它們相對的日期,并且需要復雜的計算才能以更好的時間單位(例如天)表示。
假設您知道那是什么以及如何測量它,您可能最好堅持使用銫 133 原子的未受干擾的基態超精細躍遷頻率。
uj5u.com熱心網友回復:
這是您應該能夠適應的一種天真的方法:
let arrDatas = [39, 4, 18];
const isLeapYear = year => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
const daysInYear = year => 365 (isLeapYear(year) ? 1 : 0);
const daysInMonth = (year, month) => [31, 28 (isLeapYear(year) ? 1 : 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
let when = new Date();
let year = when.getFullYear();
let month = when.getMonth();
let totalDias = 0;
for(let i = 0; i < arrDatas[0]; i )
{
year--;
totalDias = daysInYear(year);
}
for(let i = 0; i < arrDatas[1]; i )
{
month--;
totalDias = daysInMonth(year, month);
}
totalDias = arrDatas[2];
let dataData = new Date();
dataData.setDate(dataData.getDate() - totalDias);
console.log("You were born on", dataData);
您可以使用自己的值代替arrDatas上面代碼中的虛擬值。
這沒有考慮時區變化和閏秒之類的事情。
此外,您還需要驗證您的輸入。想想有人輸入 20 年 3 個月零 5383 天。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/533666.html
上一篇:如何將Python中的時間戳從dd/mm/yyhh:mm:ss:msmsms轉換為yyyy-mm-ddhh:mm:ss:msmsmsmsmsms
