objeto de fecha
1. Objeto de fecha
Date()是一个构造函数,创建date对象时使用构造函数实例化对象
1. Obtenga la hora actual del sistema
var date = new Date(); // new Date(); 获取当前系统时间
// var date2 = new Date(15554457551111) //new Date(毫秒数) 代表创建毫秒数对象的日期对象
console.log(date); //获取到的是当前时间
// console.log(typeof date);
2. El valor original de la fecha.
获取到的是1970年1月1日至今的毫秒数
console.log(date.getTime()); // getTime(); 日期的原始值 获取到的是1970年1月1日至今的毫秒数
3. Obtén el año
console.log(date.getFullYear()); // date.getFullYear(); 获取年份
4. Obtén el mes
月份从0开始的所以要加1
console.log(date.getMonth() + 1); // date.getMonth(); 获取月份 月份从0开始的所以要加1
5. Fecha de adquisición
console.log(date.getDate()); // date.getDate(); 获取日
6. Consigue la semana
console.log(date.getDay()); //date.getDay(); 获取星期
7. Obtener horas
console.log(date.getHours()); //date.getHours(); 获取小时
8. Consigue minutos
console.log(date.getMinutes()); // date.getMinutes(); 获取分钟
9. Consigue segundos
console.log(date.getSeconds()); //date.getSeconds(); 获取秒
2. Realice el formato de fecha mediante el método del objeto de fecha.
Realizar representaciones de formato de fecha
function dateFormat(date) {
console.log(showTime(date.getHours()));
var year = date.getFullYear(); // 年
var month = showTime(date.getMonth() + 1); // 月
var week = showTime(date.getDay()); // 星期
var day = showTime(date.getDate()); // 日
var hours = showTime(date.getHours()); // 小时
var minutes = showTime(date.getMinutes()); // 分钟
var second = showTime(date.getSeconds()); // 秒
var str = '';
str = str + year + '-' + month + '-' + week + '-' + day + '-' + hours + '-' + minutes + '-' + second
document.write(str);
}
var date = new Date();
dateFormat(date);
// 封装一个不够两位数就补零的函数
function showTime(t) {
var time
time = t > 10 ? t : '0' + t
return time
}
Diagrama de efectos del formato de fecha.
Contiene una función que encapsula un relleno de ceros si no hay suficientes dos dígitos.
一个不够两位数就补零的函数
// 封装一个不够两位数就补零的函数
function showTime(t) {
var time
time = t >= 10 ? t : '0' + t
return time
}