// 年月日转GPS周 格式为 2019-11-11
function UTC2GPS(dateTime) {
var year;
var month;
var day;
var DayofYear = 0;
var DayofMonth = 0;
year = parseInt(dateTime.substr(0, 4));
month = parseInt(dateTime.substr(5, 2));
day = parseInt(dateTime.substr(8, 2));
// 从1980年到当前年的上一年经过的天数
for (var i = 1980; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
DayofYear += 366;
else
DayofYear += 365;
}
// 从一月到当前月的上一月经历的天数
for (var i = 1; i < month; i++) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
DayofMonth += 31;
else if (i == 4 || i == 6 || i == 9 || i == 11)
DayofMonth += 30;
else {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
DayofMonth += 29;
else
DayofMonth += 28;
}
}
var Day;
Day = DayofMonth + day + DayofYear - 6;
var weekNo = parseInt(Day / 7);
return weekNo;
}
// 年月日转北斗周
function UTC2BeiDou(dateTime) {
var weekNo = UTC2GPS(dateTime);
var beidouWeek = weekNo - 1356;
if (beidouWeek < 1000) {
beidouWeek = '0' + beidouWeek;
}
return beidouWeek;
}
// 北斗周转年月日
function beidou2UTC(week) {
var weekDay = (parseInt(week) + 1356) * 7;
var weekYear = 0;
var currentYear = new Date().getFullYear();
var dayofYear = 0;
for (let i = 1980; i <= currentYear; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
dayofYear += 366;
if (0 <= (dayofYear - weekDay) && (dayofYear - weekDay) <= 366) {
weekYear = i;
break;
}
} else {
dayofYear += 365;
if (0 <= (dayofYear - weekDay) && (dayofYear - weekDay) <= 365) {
weekYear = i;
break;
}
}
};
var dayofMonth = 0;
var weekDayInYear = dayofYear - weekDay;
var weekMonth = 0;
for (var i = 12; i > 0; i--) {
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) {
dayofMonth += 31;
if (0 <= (dayofMonth - weekDayInYear) && (dayofMonth - weekDayInYear) <= 31) {
console.log('单数', dayofMonth - weekDayInYear, i);
weekMonth = i;
break;
}
} else if (i == 4 || i == 6 || i == 9 || i == 11) {
dayofMonth += 30;
if (0 <= (dayofMonth - weekDayInYear) && (dayofMonth - weekDayInYear) <= 30) {
console.log('双数', dayofMonth - weekDayInYear, i);
weekMonth = i;
break;
}
} else {
if ((dayofYear % 4 == 0 && dayofYear % 100 != 0) || dayofYear % 400 == 0) {
dayofMonth += 29;
if (0 <= (dayofMonth - weekDayInYear) && (dayofMonth - weekDayInYear) <= 29) {
console.log('闰年', dayofMonth - weekDayInYear, i);
weekMonth = i;
break;
}
} else {
dayofMonth += 28;
if (0 <= (dayofMonth - weekDayInYear) && (dayofMonth - weekDayInYear) <= 28) {
console.log('非闰年', dayofMonth - weekDayInYear, i);
weekMonth = i;
break;
}
}
};
};
var weekMonthDay = dayofMonth - weekDayInYear + 6
if (weekMonth >= 1 && weekMonth <= 9) {
weekMonth = "0" + weekMonth;
}
if (weekMonthDay >= 0 && weekMonthDay <= 9) {
weekMonthDay = "0" + weekMonthDay;
}
return weekYear + '-' + weekMonth + '-' + weekMonthDay;
}
GPS周 北斗周 年月日转换
猜你喜欢
转载自blog.csdn.net/strong90/article/details/103715804
今日推荐
周排行