如何在JavaScript中获取当前年份?
// Return today's date and time
var currentTime = new Date()
// returns the month (from 0 to 11)
var month = currentTime.getMonth() + 1
// returns the day of the month (from 1 to 31)
var day = currentTime.getDate()
// returns the year (four digits)
var year = currentTime.getFullYear()
// write output MM/dd/yyyy
document.write(month + "/" + day + "/" + year)
创建一个新的Date()对象并调用getFullYear():
new Date().getFullYear() // returns the current year
示例用法:始终显示当前年份的页脚:
document.getElementById(“年”).innerHTML=新日期().getFullYear();页脚{文本对齐:居中;字体系列:无衬线;}<页脚>©唐老鸭(Donald Duck)的<span id=“year”></span></footer>
另请参见Date()构造函数的完整方法列表。
下面是获取日期的另一种方法
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)
对于当前年份,我们可以使用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);
这就是我将其嵌入并输出到HTML网页的方式:
<div class="container">
<p class="text-center">Copyright ©
<script>
var CurrentYear = new Date().getFullYear()
document.write(CurrentYear)
</script>
</p>
</div>
输出到HTML页面如下:
版权所有©2018
您可以像这样简单地使用javascript。否则,您可以使用momentJs插件来帮助大型应用程序。
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)
函数生成(类型,元素){var值=“”;var date=新日期();开关(类型){案例“日期”:value=date.getDate();//以数字形式获取日期(1-31)打破案例“日”:value=date.getDay();//以数字形式获取工作日(0-6)打破案例“FullYear”:value=date.getFullYear();//获取四位数年份(yyyy)打破case“Hours”(小时):value=date.getHours();//获取小时(0-23)打破大小写“毫秒”:value=date.getMilliseconds();//获取毫秒(0-999)打破案例“分钟”:value=date.getMinutes();//获取分钟(0-59)打破案例“月”:value=date.getMonth();//获取月份(0-11)打破大小写“秒”:value=date.getSeconds();//获取秒数(0-59)打破案例“时间”:value=date.getTime();//获取时间(自1970年1月1日起的毫秒)打破}$(元素).兄弟姐妹('span').文本(值);}李{列表样式类型:无;填充:5px;}按钮{宽度:150px;}跨度{左边距:100px;}<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js“></script><ul><li><button type=“button”onclick=“generate('Date',this)”>获取日期</button><span></span></li><li><button type=“button”onclick=“generate('Day',this)”>Get Day</button><span></span></li><li><button type=“button”onclick=“generate('FullYear',this)”>获取全年</button><span></span></li><li><button type=“button”onclick=“generate('Hours',this)”>获取小时</button><span></span></li><li><button type=“button”onclick=“generate('毫秒',this)”>获取毫秒</button><span></span></li><li><button type=“button”onclick=“generate('Minutes',this)”>获取分钟</button><span></span></li><li><button type=“button”onclick=“generate('Month',this)”>获取月份</button><span></span></li><li><button type=“button”onclick=“generate('秒',this)”>获取秒</button><span></span></li><li><button type=“button”onclick=“generate('Time',this)”>获取时间</button><span></span></li></ul>
举个例子,你可以把它放在任何你想显示它的地方,而不需要引用页脚中的脚本或其他类似答案的地方
<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
页脚上的版权说明作为示例
Copyright 2010 - <script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
TL;博士
只有当您需要基于本地机器的时区和偏移量(客户端)的当前年份时,这里找到的大多数答案才是正确的,在大多数情况下,这些偏移量不能被认为是可靠的(因为机器不同)。
可靠的来源包括:
Web服务器的时钟(但确保已更新)时间API和CDN
细节
在Date实例上调用的方法将返回一个基于计算机本地时间的值。
更多详细信息可以在“MDN web docs”:JavaScript Date对象中找到。
为了方便您,我从他们的文档中添加了一条相关注释:
(…)获取日期和时间或其组件的基本方法都在本地(即主机系统)时区和偏移中工作。
提到这一点的另一个来源是:JavaScript日期和时间对象
需要注意的是,如果某人的时钟关闭了几个小时,或者他们在不同的时区,则Date对象将创建与您自己计算机上创建的时间不同的时间。
您可以使用的一些可靠来源是:
网络服务器的时钟(先检查是否准确)时间API和CDN:https://timezonedb.com/apihttp://worldtimeapi.orghttp://worldclockapi.comhttp://www.geonames.org/export/ws-overview.html其他相关API:https://www.programmableweb.com/category/time/api
但如果你根本不关心时间的准确性,或者你的用例需要一个相对于本地机器时间的时间值,那么你可以安全地使用Javascript的Date基本方法,比如Date.now()或newDate().getFullYear()(对于当前年份)。
实例化类Date并调用其getFullYear方法以获取yyyy格式的当前年份。类似于:
let currentYear = new Date().getFullYear();
currentYear变量将保存您要查找的值。
如果您将ES6 Javascript与Angular、React、VueJS等框架一起使用。然后,您应该集成第三方实用程序库,以方便您的项目。DayJS是最流行的轻量级库之一,具有不可变的数据结构。在dayJS中,您可以在一行简单的代码中获得年份,如下所示。
dayjs().year()
还有很多有用的方法。所以我建议你在下一个项目中使用dayjs。
您可以用一行JS代码获取当前年份。
<p>版权所有<script>document.write(newDate().getFullYear())</脚本></p>
抓住这个类,我们可以用javascript中的year替换它的textContent:document.querySelector(“.yr”).textContent=(new Date().getFullYear());&副本<span class=“yr”></span>公司。保留所有权利。
推荐文章
- 在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对象平放/不平放的最快方法