1. クリックして年に 1 つ追加します
2. 年から 1 を引いた値をクリックします。
3. クリックして 1 か月を追加します
4. 月から 1 を引いた値をクリックします。
5. 日数プラス 1 をクリックします。
6. 日数から 1 を引いた数をクリックします。
7. checkMonth 関数の機能は、1 桁の月の前に「0」を追加することです。たとえば、「7」は「07」になります。
8. 時間変換機能。
1. 日付換算関数(xx年xx月xx日xx時xx分xx秒)。
2. 時間帯ごとに○○日、○○時間、○○分の差を求める
data() {
return {
yearMonthValue: "2021-12-23",
};
},
1. クリックして年に 1 つ追加します
// 点击年份加一
YearAdd() {
let currentDate = this.yearMonthValue;
currentDate = new Date(currentDate);
let nextDate = currentDate.setYear(currentDate.getFullYear() + 1); // 输出日期格式为毫秒形式1556668800000
nextDate = new Date(nextDate);
console.log(nextDate);
this.yearMonthValue = nextDate;
},
2.年から 1 を引いた値をクリックします。
// 点击年份减一
YearReduce() {
let currentDate = this.yearMonthValue;
currentDate = new Date(currentDate);
let lastDate = currentDate.setYear(currentDate.getFullYear() - 1); // 输出日期格式为毫秒形式1556668800000
lastDate = new Date(lastDate);
console.log(lastDate);
this.yearMonthValue = lastDate;
},
3.クリックして 1 か月を追加します
// 点击月份加一
MonthAdd() {
let currentDate = this.yearMonthValue;
currentDate = new Date(currentDate);
let nextDate = currentDate.setMonth(currentDate.getMonth() + 1); // 输出日期格式为毫秒形式1556668800000
nextDate = new Date(nextDate);
let nextYear = nextDate.getFullYear();
let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
nextDate = nextYear + "-" + nextMonth; // "2019-05"
console.log(nextDate);
this.yearMonthValue = nextDate;
},
4.月から 1 を引いた値をクリックします。
// 点击月份减一
MonthReduce() {
let currentDate = this.yearMonthValue;
currentDate = new Date(currentDate);
let lastDate = currentDate.setMonth(currentDate.getMonth() - 1); // 输出日期格式为毫秒形式1551398400000
lastDate = new Date(lastDate);
let lastYear = lastDate.getFullYear();
let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
lastDate = lastYear + "-" + lastMonth; // "2019-03"
console.log(lastDate);
this.yearMonthValue = lastDate;
},
5.日数プラス 1 をクリックします。
//点击天数加一
DayAdd(){
let currentDate = this.yearMonthValue;
currentDate = new Date(currentDate);
let nextDate = currentDate.setMonth(currentDate.getMonth()); // 输出日期格式为毫秒形式1556668800000
nextDate = new Date(nextDate);
let nextYear = nextDate.getFullYear();
let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
let nextDay = currentDate.setDate(currentDate.getDate() + 1); // 输出日期格式为毫秒形式1556668800000
nextDay = new Date(nextDay);
let nextday = nextDay.getDate()
nextDate = nextYear + "-" +nextMonth+"-"+nextday; // "2019-05"
console.log(nextDate);
this.yearMonthValue = nextDate;
},
6.日数から 1 を引いた数をクリックします。
//点击天数减一
DayReduce(){
let currentDate = this.yearMonthValue;
currentDate = new Date(currentDate);
let nextDate = currentDate.setMonth(currentDate.getMonth()); // 输出日期格式为毫秒形式1556668800000
nextDate = new Date(nextDate);
let nextYear = nextDate.getFullYear();
let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
let nextDay = currentDate.setDate(currentDate.getDate() - 1); // 输出日期格式为毫秒形式1556668800000
nextDay = new Date(nextDay);
let nextday = nextDay.getDate()
nextDate = nextYear + "-" +nextMonth+"-"+nextday; // "2019-05"
console.log(nextDate);
this.yearMonthValue = nextDate;
},
7. checkMonth 関数の機能は、1 桁の月の前に「0」を追加することです。たとえば、「7」は「07」になります。
//checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’
checkMonth(i) {
if (i < 10) {
i = "0" + i;
}
return i;
},
換算日付関数(xx年xx月xx日xx時xx分xx秒)。
function filterTime(time) {
var date = new Date(time);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
var h = date.getHours();
h = h < 10 ? "0" + h : h;
var minute = date.getMinutes();
minute = minute < 10 ? "0" + minute : minute;
var s = date.getSeconds();
s = s < 10 ? "0" + s : s;
return y + "年" + m + "月" + d + "日" + " " + h + ":" + minute;
}
期間に応じて、○○日、○○時間、○○分の差を求める
// 根据时间段获取相差XX天xx小时xx分钟
function formatDuring(millisecond) {
var days = parseInt(millisecond / (1000 * 60 * 60 * 24));
var hours = parseInt(
(millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
var minutes = parseInt((millisecond % (1000 * 60 * 60)) / (1000 * 60));
var seconds = (millisecond % (1000 * 60)) / 1000;
return days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";
}
参考資料:【応用】JSを使って月ごとに日付を足したり引いたりする方法_Dora_5537のブログ - CSDN blog_js month plus one
役に立ちましたら、いいね、フォロー、お気に入り登録をお願いします(。・ω・。)ノ♡