Time, Date full (ok)
Last updated
Last updated
Đọc thêm:
a1
Part 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var test1 = moment('2015-07-04T00:00:00.000Z').utc().format('MM/DD/YYYY HH:mm:ss');
var test2 = moment('2015-07-04T00:00:20.000Z').utc().format('MM/DD/YYYY HH:mm:ss');
var data1 = new Date(test1);
var data2 = new Date(test2);
console.log(data1.getTime());
console.log(data2.getTime());
</script>
// 1435942800000
// 1435942820000
</body>
</html>
Part 2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var proposedM1 = moment('2015-07-05T09:30:00.000Z').utc().format('MM/DD/YYYY HH:mm:ss');
var momentDate = moment(proposedM1)
var hours = momentDate.hours();
var minutes = momentDate.minutes();
console.log(hours);
console.log(minutes);
// 9
// 30
</script>
</body>
</html>
Part 3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var DateDiff = {
inSeconds: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/1000);
},
inMinutes: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2-t1)/60000);
},
inMinutesRound: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
var muni = parseInt((t2-t1)/(60000));
var mod = muni % 60;
return mod;
},
inHous: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / (3600 * 1000));
},
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / (24 * 3600 * 1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / (24 * 3600 * 1000 * 7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M + 12 * d2Y) - (d1M + 12 * d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear() - d1.getFullYear();
}
}
var dString = "Dec, 10, 2021";
var d1 = new Date(dString);
var d2 = new Date();
document.write("<br />Number of <b>seconds</b> since " + dString + ": " + DateDiff.inSeconds(d1, d2));
document.write("<br />Number of <b>minutes</b> since " + dString + ": " + DateDiff.inMinutes(d1, d2));
document.write("<br />Number of <b>minutesRound</b> since " + dString + ": " + DateDiff.inMinutesRound(d1, d2));
document.write("<br />Number of <b>hous</b> since " + dString + ": " + DateDiff.inHous(d1, d2));
document.write("<br />Number of <b>days</b> since " + dString + ": " + DateDiff.inDays(d1, d2));
document.write("<br />Number of <b>weeks</b> since " + dString + ": " + DateDiff.inWeeks(d1, d2));
document.write("<br />Number of <b>months</b> since " + dString + ": " + DateDiff.inMonths(d1, d2));
document.write("<br />Number of <b>years</b> since " + dString + ": " + DateDiff.inYears(d1, d2));
</script>
Number of seconds since Dec, 10, 2021: 39548
Number of seconds since Dec, 10, 2021: 39548
Number of minutes since Dec, 10, 2021: 659
Number of minutesRound since Dec, 10, 2021: 59
Number of hous since Dec, 10, 2021: 10
Number of days since Dec, 10, 2021: 0
Number of weeks since Dec, 10, 2021: 0
Number of months since Dec, 10, 2021: 0
Number of years since Dec, 10, 2021: 0
</body>
</html>
Part 4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var DateDiff = {
inSeconds: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / 1000);
},
inMinutes: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / 60000);
},
inMinutesRound: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
var muni = parseInt((t2 - t1) / (60000));
var mod = muni % 60;
return mod;
},
inHous: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / (3600 * 1000));
},
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / (24 * 3600 * 1000));
},
inWeeks: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
return parseInt((t2 - t1) / (24 * 3600 * 1000 * 7));
},
inMonths: function(d1, d2) {
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
return (d2M + 12 * d2Y) - (d1M + 12 * d1Y);
},
inYears: function(d1, d2) {
return d2.getFullYear() - d1.getFullYear();
}
}
var test1 = moment('2021-12-11T00:00:00Z').utc().format('MM/DD/YYYY HH:mm:ss');
var test2 = moment('2021-12-11T01:00:00Z').utc().format('MM/DD/YYYY HH:mm:ss');
var d1 = new Date(test1);
var d2 = new Date(test2);
var momentDate1 = moment(d1)
var hours1 = momentDate1.hours();
var minutes1 = momentDate1.minutes();
var momentDate2 = moment(d2)
var hours2 = momentDate2.hours();
var minutes2 = momentDate2.minutes();
document.write("<br />Number of <b>minutes</b> since: " + DateDiff.inMinutes(d1, d2));
document.write("<br />Number of <b>seconds</b> since: " + DateDiff.inSeconds(d1, d2));
</script>
Number of minutes since: 60
Number of seconds since: 3600
</body>
</html>
Part 5
<!DOCTYPE html>
<html>
<head>
<!-- jQuery and jQueryUI References -->
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type='text/javascript'>
var start = new Date("7/14/2014 08:00");
var end = new Date("7/15/2014 10:00");
var working_start = 8;
var working_end = 10;
var includeWeekends = false;
var result = workingHoursBetweenDates(start, end, working_start, working_end, includeWeekends);
console.log(result);
var working_end_test = new Date("1/1/0001 " + "17:00").getHours() + (new Date("1/1/0001 " + "17:30").getMinutes() / 60);
console.log(working_end_test);
// Simple function that accepts two parameters and calculates the number of hours worked within that range
function workingHoursBetweenDates(startDate, endDate, dayStart, dayEnd, includeWeekends) {
// Store minutes worked
var minutesWorked = 0;
// Validate input
if (endDate <= startDate) {
return 0;
}
// Loop from your Start to End dates (by hour)
var current = startDate;
// Define work range
var workHoursStart = dayStart;
var workHoursEnd = dayEnd;
// Loop while currentDate is less than end Date (by minutes)
while (current <= endDate) {
// Is the current time within a work day (and if it occurs on a weekend or not)
var currentTime = current.getHours() + (current.getMinutes() / 60);
if (currentTime >= workHoursStart && currentTime < workHoursEnd && (includeWeekends ? current.getDay() !== 0 && current.getDay() !== 6 : true)) {
// Increment the number of minutes worked
minutesWorked++;
}
// Increment current time
current.setTime(current.getTime() + 1000 * 60);
}
// Return the number of hours
return Math.round(minutesWorked / 60 * 100) / 100;
}
</script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
</body>
</html>
Part 6 Converted date: 2021-12
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
//a simple date formatting function
function dateFormat(inputDate, format) {
//parse the input date
const date = new Date(inputDate);
//extract the parts of the date
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
//replace the month
format = format.replace("MM", month.toString().padStart(2, "0"));
//replace the year
if (format.indexOf("yyyy") > -1) {
format = format.replace("yyyy", year.toString());
} else if (format.indexOf("yy") > -1) {
format = format.replace("yy", year.toString().substr(2, 2));
}
//replace the day
format = format.replace("dd", day.toString().padStart(2, "0"));
return format;
}
console.log('Converted date: ' + dateFormat('2021-12-10', 'yyyy-MM'));
</script>
</body>
</html>
Part 7: Tính số năm công tác của nhân viên
<!DOCTYPE html>
<html>
<body>
<h2>My First JavaScript</h2>
<script>
function affixZero(int) {
if (int < 10) { int = '0' + int; }
return String(int);
}
function calculateAgeOrYear(past,next) {
var dateRegex = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
if (!(dateRegex.test(past))) {
return '';
}
var birth = past.split('-');
var _birth = parseInt(birth[0] + birth[1] + birth[2]);
var today = new Date();
if(next)
today = new Date(next);
var _today = parseInt(today.getFullYear() + affixZero(today.getMonth() + 1) + affixZero(today.getDate()));
return parseInt((_today - _birth) / 10000);
}
var arss = calculateAgeOrYear('1986-05-18','1988-05-20');
console.log(arss);
</script>
</body>
</html>
/*Hàm tính số ngày trong tháng javascript*/
const get_day_of_month = (year, month) => {
return new Date(year, month, 0).getDate();
};
const get_day_of_month = (year, month) => {
return new Date(year, month, 0).getDate();
};
let thang2 = get_day_of_month(2021, 2); //28
let thang3 = get_day_of_month(2021, 3); //31
/*Hàm tính khoảng cách giữa 2 ngày trong javascript*/
const get_day_of_time = (d1, d2) => {
let ms1 = d1.getTime();
let ms2 = d2.getTime();
return Math.ceil((ms2 - ms1) / (24*60*60*1000));
};
const get_day_of_time = (d1, d2) => {
let ms1 = d1.getTime();
let ms2 = d2.getTime();
return Math.ceil((ms2 - ms1) / (24*60*60*1000));
};
let birthday = new Date('2019-10-08');
console.log(birthday.toUTCString());
//Tue, 08 Oct 2019 00:00:00 GMT
let today = new Date();
console.log(today.toUTCString());
//Wed, 22 Dec 2021 01:30:49 GMT
let time = get_day_of_time(birthday,today)
console.log(time+ ' day');
//807 day
j