html New date获取日,js中的New Date获取各种时间

这篇博客展示了如何在JavaScript中创建Date对象并进行时间操作,包括获取日期、时间、年份、月份等信息,并进行了时间的格式化。还演示了如何设置年份和日期,以及处理日期和月份的边界情况。

var thedate = new Date();

//alert(thedate.toLocaleString());

//alert(thedate);

//alert('LocaleString:'+thedate.toLocaleString());//将时间转换成本地的字符串表示

//        alert('String:' + thedate.toString());

//        alert('LocaleDateString:' + thedate.toLocaleDateString());

//        alert('LocaleTimeString:' + thedate.toLocaleTimeString());

//        alert(thedate.toTimeString());

alert('Date:' + thedate.getDate());

//        alert('Day:' + thedate.getDay());

alert('Year:' + thedate.getFullYear());

//alert('当前日期为' + thedate.getFullYear(), thedate.getMonth(), thedate.getDate(), thedate.getHours() + '时' + thedate.getMinutes() + '分' + thedate.getSeconds() + '秒');

//        alert('Milliseconds:' + thedate.getMilliseconds());

// alert('Minutes:' + thedate.getMinutes()+'分');

//        alert('Month:' + (thedate.getMonth()+1));//如果用整数表示月份的话,0代表1月,1代表2月...11代表12月

//alert('Seconds:' + thedate.getSeconds()+'秒');

//var thedate = new Date(2012, 3, 7, 12, 11, 12,222);

//        alert(thedate.toLocaleString());

//        alert(thedate.getMilliseconds());

/*var thedate = new Date('2012, March, 7,12:11:22');

alert(thedate.toLocaleString());

alert(thedate.getMilliseconds());

alert(thedate.getYear()); //建议使用GetFullYear获取年*/

var thedate = new Date('2012, March, 7,12:11:22');

thedate.setFullYear(2013);

alert(thedate.toLocaleString());

alert(thedate.toLocaleString());

thedate.setFullYear(2013,3,8);

alert(thedate.toLocaleString());

thedate.setDate(1);

alert(thedate.toLocaleString());

thedate.setMonth(17);

alert(thedate.toLocaleString());

thedate.setDate(365);

thedate.setDate(32);

alert(thedate.toLocaleString());

### JavaScript 中使用 `new Date` 获取年月的指南与代码示例 在 JavaScript 中,`Date` 对象提供了多种方法来获取当前期的年份、月份和期。以下是一个完整的代码示例以及详细的说明。 ```javascript // 创建一个新的 Date 对象,表示当前期和时间 const now = new Date(); // 获取年份(四位数) const year = now.getFullYear(); // 获取月份(注意:getMonth() 返回的值是从 0 开始的,因此需要加 1) const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取当前月份中的期(即几号) const day = String(now.getDate()).padStart(2, '0'); // 将年、月、格式化为常见的 YYYY-MM-DD 格式 const formattedDate = `${year}-${month}-${day}`; console.log(formattedDate); // 输出类似:2023-10-18 ``` 上述代码中使用了 `getFullYear()` 方法来获取四位数的年份[^1]。`getMonth()` 方法返回的是从 0 到 11 的整数,因此需要加上 1 来得到实际的月份[^3]。为了确保月份和期始终是两位数格式,使用了 `String.prototype.padStart(2, '0')` 方法[^5]。 ### 获取当月最后一天的扩展示例 如果需要获取当前月份的最后一天,可以参考以下代码: ```javascript function getLastDay(year, month) { const newDate = new Date(year, month + 1, 0); // 下一个月的第一天减去一天 return newDate.getDate(); } const currentYear = new Date().getFullYear(); const currentMonth = new Date().getMonth(); console.log(getLastDay(currentYear, currentMonth)); // 输出当前月份的最后一天 ``` 此代码通过创建一个新期对象并设置为下一个月的第一天,然后减去一天,从而获得当前月份的最后一天[^2]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值