var date = new Date();
date.getYear(); //获取当前年份(2位)
date .getFullYear(); //获取完整的年份(4位)
date.getMonth(); //获取当前月份(0-11,0代表1月)
date.getDate(); //获取当前日(1-31)
date.getDay(); //获取当前星期X(0-6,0代表星期天)
date.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
date.getHours(); //获取当前小时数(0-23)
date.getMinutes(); //获取当前分钟数(0-59)
date.getSeconds(); //获取当前秒数(0-59)
date.getMilliseconds(); //获取当前毫秒数(0-999)
date.toLocaleDateString(); //获取当前日期
var mytime=date .toLocaleTimeString(); //获取当前时间
date.toLocaleString( ); //获取日期与时间
// 获取当前月份
var nowMonth = date.getMonth() + 1;
// 获取当前是几号
var strDate = date.getDate();
// 添加分隔符“-”
var seperator = "-";
// 对月份进行处理,1-9月在前面添加一个“0”
if (nowMonth >= 1 && nowMonth <= 9) {
nowMonth = "0" + nowMonth;
}
// 对月份进行处理,1-9号在前面添加一个“0”
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
// 最后拼接字符串,得到一个格式为(yyyy-MM-dd)的日期
var nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate;
// 获取的是前一天日期
var time = (new Date).getTime() - 24 * 60 * 60 * 1000;
var yesday = new Date(time); // 获取的是前一天日期
//获取时间戳精确到毫秒
const timestamp = Date.parse(new Date());
console.log(timestamp);
//输出 13位
————————————————
`
JS获取当前时间的年、月、日、时间等
猜你喜欢
转载自blog.csdn.net/qq_38881495/article/details/129553006
今日推荐
周排行