JavaScript日期获得有甚么方法
JavaScript中有多种方法可以获得日期,以下是一些经常使用的方法:
Date()
构造函数获得当前日期和时间:const currentDate = new Date();
getFullYear()
、getMonth()
、getDate()
等方法获得特定日期的年、月、日等信息:const year = currentDate.getFullYear();
const month = currentDate.getMonth(); // 月份从0开始,0表示一月
const day = currentDate.getDate();
getDay()
方法获得星期几的数值(0表示星期日,1表示星期一,以此类推):const dayOfWeek = currentDate.getDay();
toLocaleString()
方法获得本地化的日期和时间字符串:const localDateTimeString = currentDate.toLocaleString();
toUTCString()
方法获得UTC格式的日期和时间字符串:const utcDateTimeString = currentDate.toUTCString();
getTime()
方法获得距离1970年1月1日的毫秒数:const milliseconds = currentDate.getTime();
const formattedDate = moment(currentDate).format('YYYY-MM-DD');
这些只是JavaScript中获得日期的经常使用方法,还有其他一些方法可以根据具体需求使用。
TOP