<!DOCTYPE html><html><head><meta charset="UTF-8" /><meta name="viewport" content="width=device-width" /><title>日期函数</title></head><body><script type="text/javascript">// 常用的日期函数如下表所示//// [caption=]// [cols="1,1",options="header",stripes=even]// |===// | 方 法 | 描 述// | getFullYear() | 获取完整的年份(4位,1970年至?)// | getMonth() | 获取当前月份(0~11, 0 代表 1 月)// | getDate() | 获取当前日(1~31)// | getDay | 获取当前星期 X(0~6, 0 代表星期天)// | getTime() | 获取当前时间(从1970年1月1日 开始的毫秒数)// | getHours() | 获取当前小时数(0~23)// | getMinutes() | 获取当前分数(0~59)// | getSeconds | 获取当前秒数(0~59)// | toLocalDateString() | 获取当前日期// | toLocalTimeString() | 获取当前时间// | toLocaleString() | 获取日期与时间// |===//// [caption=]// .示例代码// [cols="6,4"]// |===// a|[source,javascript,linenums]// ----var dateNow = new Date(); // 获取当前时间的日期对象var mydate = new Date('2023年1月28日 15:19:15');console.log("年:" + mydate.getFullYear());console.log('月: '+ (mydate.getMonth()+1)); // <1>console.log('日:' + mydate.getDate());console.log('星期:' + mydate.getDay());console.log('时间戳:'+ mydate.getTime()); // <2>console.log('小时:' + mydate.getHours());console.log('分:' + mydate.getMinutes());console.log('秒:' + mydate.getSeconds());console.log('日期:' + mydate.toLocaleDateString());console.log('时间:' + mydate.toLocaleTimeString());console.log('日期与时间:' + mydate.toLocaleString());// ----//// .^a|....// 年:2023// 月: 1// 日:28// 星期:6// 时间戳:1674890355000// 小时:15// 分:19// 秒:15// 日期:1/28/2023// 时间:3:19:15 PM// 日期与时间:1/28/2023, 3:19:15 PM// ....// |===//// <1> 月份为0~11,所以必须加 1// <2> 从 1970 年 1 月 1 日 0 点 0 分 0 秒到现在的毫秒数</script></body></html>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。