js教程

javascript获取当前时间方法汇总

我的站长站 2023-09-28 人阅读

1. Date()方法

Date() 函数用于创建一个包含当前日期和时间的新 Date 对象。

const now = new Date();
console.log(now);

输出:

Wed Sep 15 2021 15:21:45 GMT+0800 (中国标准时间)

2. getTime()方法

getTime() 函数返回从 1970 年 1 月 1 日到现在的毫秒数。

const now = new Date();
const timestamp = now.getTime();
console.log(timestamp);

输出:

1631692896415

3. getFullYear()方法

getFullYear() 函数返回当前年份。

const now = new Date();
const year = now.getFullYear();
console.log(year);

输出:

2021

4. getDate()方法

getDate() 函数返回当前月份的日数。

const now = new Date();
const day = now.getDate();
console.log(day);

输出:15

5. getMonth()方法

getMonth() 函数返回当前月份的数字(0-11)。

const now = new Date();
const month = now.getMonth();
console.log(month);

输出:

8

6. getDay()方法

getDay() 函数返回当前星期的数字(0-6)。

const now = new Date();
const weekday = now.getDay();
console.log(weekday);

输出:

3

7. getHours()方法

getHours() 函数返回当前时间的小时数。

const now = new Date();
const hours = now.getHours();
console.log(hours);

输出:15

8. getMinutes()方法

getMinutes() 函数返回当前时间的分钟数。

const now = new Date();
const minutes = now.getMinutes();
console.log(minutes);

输出:31

9. getSeconds()方法

getSeconds() 函数返回当前时间的秒数。

const now = new Date();
const seconds = now.getSeconds();
console.log(seconds);

输出:13

10. getMilliseconds()方法

getMilliseconds() 函数返回当前时间的毫秒数。

const now = new Date();
const milliseconds = now.getMilliseconds();
console.log(milliseconds);

输出:

974
相关推荐
  • JS时间
  • JS本站已运行时间统计代码

    JS本站已运行时间统计代码<SCRIPT language=javascript>document.write("");function show_date_time(){window.setTimeout("show_date_time()", 1000);BirthDay=new Date("11-30-2011");//改成你的论坛建站的日期月/日/年today=new Date();timeold=...

    js教程 134 3年前
  • JS年月日星期早中午时间代码

    JS年月日星期早中午时间代码,我的站长站自用时间代码,完美简单。var day=""; var month=""; var ampm=""; var ampmhour=""; var myweekday=""; var year=""; var hh;var wh;mydate=new Date(); myweekday=mydate.getDay(); mymonth=mydate.g...

    js教程 81 3年前
  • javascript获取当前时间方法汇总

    1. Date()方法Date() 函数用于创建一个包含当前日期和时间的新 Date 对象。const now = new Date();console.log(now);输出:Wed Sep 15 2021 15:21:45 GMT+0800 (中国标准时间)2. getTime()方法getTime() 函数返回从 1970 年 1 月 1 日到现在的毫秒数...

    js教程 33 7个月前
最新更新
  • js截取字符串教程

    slice()方法接受两个参数,起始索引和结束索引(可选)。它返回从起始索引到结束索引(不包括结束索引)之间的子字符...

    js教程 3个月前
  • find findIndex indexOf索引选择器使用方法

    find使用方法find方法是ES6引入的一种数组方法,可以用来查找数组中符合条件的元素。语法是:array.find(callba...

    js教程 3个月前
  • js复制网页内容教程

    Async Clipboard API方法HTML5新增的方法,无需引入第三方插件,直接就可以复制内容。低版本的浏览器可能会不兼...

    js教程 3个月前
  • js获取字符长度函数分享

    js获取字符长度函数function objLen(str) { if (str == null) return 0; if (typeof str != "string") { ...

    js教程 3个月前
  • 网站LED跑马灯效果广告代码

    网站可以看到很多的论坛网站都会用到这种网站LED跑马灯效果,这种效果实现也很简单,分享给大家。LED跑马灯效果...

    js教程 4个月前