js获取时间及转化

有关Javascript的本地时间获取,下面我直接用代码来进行举例吧

1     let time = new Date();
2     let month = time.getMonth()+1;  //获取的月份0~11要进行加1
3     let day = time.getDate();       //获取天数
4     let DataMonth = month<10?("0" + month):month;  //小于10的在前面进行加0处理
5     let DataDay = day<10?("0" + day):day;   //同上
6     let Data = DataMonth.concat(DataDay);  //进行个拼接(也可以直接使用加法)
7 
8 输出结果为:0201
9 new Date(2020,02,01);   //获取传入日期的时间,也可以直接是变量

以下是我们常用到获取时间的方法,具体的用法和其它属性请看官网

getDate()从 Date 对象返回一个月中的某一天 (1 ~ 31)。

getDay()从 Date 对象返回一周中的某一天 (0 ~ 6)。

getMonth()从 Date 对象返回月份 (0 ~ 11)。

getFullYear()从 Date 对象以四位数字返回年份。

getHours()返回 Date 对象的小时 (0 ~ 23)。

getMinutes()返回 Date 对象的分钟 (0 ~ 59)。

getSeconds()返回 Date 对象的秒数 (0 ~ 59)。

原文地址:https://www.cnblogs.com/dy105525/p/12248312.html