JS 将字符串形式的日期转换成日期类型

1、将字符串形式的日期转换成日期对象

var strTime="2011-04-16"; //字符串日期格式  
var date=new Date(Date.parse(strTime.replace(/-/g,"/")));//转换为Dat()

2、获取当前月份

var currentMonth=date.getMonth()+1;//获取当前月份

3、

date.getYear();        //获取当前年份(2位)
date.getFullYear();    //获取完整的年份(4位,1970-????)
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( );        //获取日期与时间

4、时间

//转换成时间戳
let endTime = val.valueOf()
//获取当天0点时间戳
const date = new Date(new Date().toLocaleDateString()).getTime()

猜你喜欢

转载自blog.csdn.net/renfeideboke/article/details/128459198