如何计算 Javascript 与 HTML 周表格的周差?
How do I calculate week difference in Javascript from the HTML week form?
我正在寻找一种方法来根据另一个周表单元素的输入来填充一个周表单元素。我是 Javascript.
的新手
基本上我需要得到 c1 和 c2 表格之间的周差。
然后用相同的周差自动填充 d1 和 d2,但 d1 在 c2 之后一周开始。
示例:
c1 = "2018-W01"
c2 = "2018-W05"
应将 d1 和 d2 值设置为:
d1 = "2018-W06"
d2 = "2018-W10"
<input type="week" id="c1">
<input type="week" id="c2">
<input type="week" id="d1" readonly>
<input type="week" id="d2" readonly>
<script>
function myFunction() {
var d1 = document.getElementById("c1").value;
var d2 = document.getElementById("c2").value;
var year = d1.substring(0,4); //2018
var day1 = d1.substring(6,); //01
var day2 = d2.substring(6,); //05
var difference = day2 - day1; //5
document.getElementById("d1").value = d1 + difference; // ?
document.getElementById("d2").value = d2 + difference;
</script>
这段代码显然行不通,但表达了我的想法。
对于两年之间重叠的日期,这确实会产生问题。
感谢您的帮助!
在JS中可以使用
https://momentjs.com/
此库解决了您在日期
上进行任何操作的问题
改变这两行
var day1 = parseInt(d1.substring(6,)); //1
var day2 = parseInt(d2.substring(6,)); //5
那么应该可以了
但最好使用 Javascript 日期 -> 很好的参考 https://www.w3schools.com/js/js_dates.asp
试试这个。
我想要完整的年份(包含 52 周),所以我使用了
year2-(year1+1),
为了排除这两年,然后我添加了 year1 周 (52-week1)
然后添加week2
var year1 = d1.substring(0,4); //2017
var year2 = d1.substring(0,4); //2018
var week1 = d1.substring(6,); //01
var week2 = d2.substring(6,); //05
let result = 52 *( year2 - (year1 + 1) ) + (52 - week1) + week2
Hope, i could help you... I had use this in one of my project.
function diff_weeks(dt2, dt1){
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60 * 24 * 7);
return Math.abs(Math.round(diff));
}
dt1 = new Date(2014,10,4);
dt2 = new Date(2014,10,12);
console.log(diff_weeks(dt1, dt2));
dt1 = new Date("June 4, 1990 08:11:00");
dt2 = new Date("October 10, 2018 11:13:00");
console.log(diff_weeks(dt1, dt2));
momentjs 是您应该用来管理日期和时间的库。
它将帮助您以简单的方式管理日期的每个值。
var a = moment('2016-01-01');
var b = a.add(1, 'week');
a.format();
"2016-01-08T00:00:00-06:00"
看这个例子:
试试这个..
function myFunction(d1, d2) {
var year1 = parseInt(d1.substring(0, 4)); //2018
var year2 = parseInt(d2.substring(0, 4));
var yearDiff = Math.abs(year1 - year2);
var weeksInYears = 52 * yearDiff;
var day1 = parseInt(d1.substring(6, )); //01
var day2 = parseInt(d2.substring(6, )); //05
var difference = Math.abs(weeksInYears - (day2 - day1)); // handled dates with different years
var week1 = day2 + 1;
var week2 = week1 + difference;
week1 = ((week1 < 10 ? '0' : '') + week1);
week2 = ((week2 < 10 ? '0' : '') + week2)
var res = year1 + "-" + "W" + (week1); // ?
var res1 = year2 + "-" + "W" + (week2);
console.log(res, res1);}
我正在寻找一种方法来根据另一个周表单元素的输入来填充一个周表单元素。我是 Javascript.
的新手基本上我需要得到 c1 和 c2 表格之间的周差。 然后用相同的周差自动填充 d1 和 d2,但 d1 在 c2 之后一周开始。
示例:
c1 = "2018-W01"
c2 = "2018-W05"
应将 d1 和 d2 值设置为:
d1 = "2018-W06"
d2 = "2018-W10"
<input type="week" id="c1">
<input type="week" id="c2">
<input type="week" id="d1" readonly>
<input type="week" id="d2" readonly>
<script>
function myFunction() {
var d1 = document.getElementById("c1").value;
var d2 = document.getElementById("c2").value;
var year = d1.substring(0,4); //2018
var day1 = d1.substring(6,); //01
var day2 = d2.substring(6,); //05
var difference = day2 - day1; //5
document.getElementById("d1").value = d1 + difference; // ?
document.getElementById("d2").value = d2 + difference;
</script>
这段代码显然行不通,但表达了我的想法。 对于两年之间重叠的日期,这确实会产生问题。 感谢您的帮助!
在JS中可以使用 https://momentjs.com/
此库解决了您在日期
上进行任何操作的问题改变这两行
var day1 = parseInt(d1.substring(6,)); //1
var day2 = parseInt(d2.substring(6,)); //5
那么应该可以了 但最好使用 Javascript 日期 -> 很好的参考 https://www.w3schools.com/js/js_dates.asp
试试这个。
我想要完整的年份(包含 52 周),所以我使用了
year2-(year1+1),
为了排除这两年,然后我添加了 year1 周 (52-week1)
然后添加week2
var year1 = d1.substring(0,4); //2017
var year2 = d1.substring(0,4); //2018
var week1 = d1.substring(6,); //01
var week2 = d2.substring(6,); //05
let result = 52 *( year2 - (year1 + 1) ) + (52 - week1) + week2
Hope, i could help you... I had use this in one of my project.
function diff_weeks(dt2, dt1){
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60 * 24 * 7);
return Math.abs(Math.round(diff));
}
dt1 = new Date(2014,10,4);
dt2 = new Date(2014,10,12);
console.log(diff_weeks(dt1, dt2));
dt1 = new Date("June 4, 1990 08:11:00");
dt2 = new Date("October 10, 2018 11:13:00");
console.log(diff_weeks(dt1, dt2));
momentjs 是您应该用来管理日期和时间的库。
它将帮助您以简单的方式管理日期的每个值。
var a = moment('2016-01-01');
var b = a.add(1, 'week');
a.format();
"2016-01-08T00:00:00-06:00"
看这个例子:
试试这个..
function myFunction(d1, d2) {
var year1 = parseInt(d1.substring(0, 4)); //2018
var year2 = parseInt(d2.substring(0, 4));
var yearDiff = Math.abs(year1 - year2);
var weeksInYears = 52 * yearDiff;
var day1 = parseInt(d1.substring(6, )); //01
var day2 = parseInt(d2.substring(6, )); //05
var difference = Math.abs(weeksInYears - (day2 - day1)); // handled dates with different years
var week1 = day2 + 1;
var week2 = week1 + difference;
week1 = ((week1 < 10 ? '0' : '') + week1);
week2 = ((week2 < 10 ? '0' : '') + week2)
var res = year1 + "-" + "W" + (week1); // ?
var res1 = year2 + "-" + "W" + (week2);
console.log(res, res1);}