如何在JavaScript中获取当前年份?
当前回答
抓住这个类,我们可以用javascript中的year替换它的textContent:document.querySelector(“.yr”).textContent=(new Date().getFullYear());&副本<span class=“yr”></span>公司。保留所有权利。
其他回答
举个例子,你可以把它放在任何你想显示它的地方,而不需要引用页脚中的脚本或其他类似答案的地方
<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
页脚上的版权说明作为示例
Copyright 2010 - <script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
对于当前年份,我们可以使用Date类中的getFullYear(),但是有很多函数可以根据需要使用,
var now=新日期()console.log(“当前时间为:”+now);//getFullYear函数将给出当前年份var currentYear=now.getFullYear()console.log(“当前年份为:”+currentYear);//getYear将为您提供1990年后的年份,即当前年份-1990年var year=now.getYear()console.log(“当前年份为:”+年);//getMonth提供月份值,但月份从0开始//加1得到实际月份值var month=now.getMonth()+1console.log(“当前月份为:”+月份);//getDate给出日期值var day=now.getDate()console.log(“今天是:”+day);
下面是获取日期的另一种方法
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)
您可以用一行JS代码获取当前年份。
<p>版权所有<script>document.write(newDate().getFullYear())</脚本></p>
实例化类Date并调用其getFullYear方法以获取yyyy格式的当前年份。类似于:
let currentYear = new Date().getFullYear();
currentYear变量将保存您要查找的值。
推荐文章
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?
- angularjs中的compile函数和link函数有什么区别
- 删除绑定中添加的事件监听器
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法