Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取JavaScript中的当前年份_Javascript_Date - Fatal编程技术网

获取JavaScript中的当前年份

获取JavaScript中的当前年份,javascript,date,Javascript,Date,如何使用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.getDat

如何使用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)
new Date().getFullYear()
// returns the current year

劫持已接受的答案,以提供一些基本示例上下文,如始终显示当前年份的页脚:

<footer>
    &copy; <span id="year"></span>
</footer>
页脚{
文本对齐:居中;
字体系列:无衬线;
}

&抄袭;福巴2018

这里是另一种获取日期的方法

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)

对于本年度,我们可以使用从日期开始的getFullYear()类,但是有许多函数可以根据需要使用,有些函数如下:

var now=新日期()
console.log(“当前时间为:“+now”);
//getFullYear函数将给出当前年份
var currentYear=now.getFullYear()
console.log(“本年度为:“+本年度”);
//getYear将为您提供1990年之后的年份,即当前年份-1990年
var year=now.getYear()
console.log(“本年为:“+年”);
//getMonth提供月份值,但月份从0开始
//添加1以获得实际月份值
var month=now.getMonth()+1
console.log(“当前月份为:“+月份”);
//getDate提供日期值
var day=now.getDate()

log(“今天是:”+天)这就是我将其嵌入并输出到HTML网页的方式:

<div class="container">
    <p class="text-center">Copyright &copy; 
        <script>
            var CurrentYear = new Date().getFullYear()
            document.write(CurrentYear)
        </script>
    </p>
</div>

版权和副本; var CurrentYear=新日期().getFullYear() 文件写入(当前年度)

输出到HTML页面如下所示:


版权所有©2018

您可以像这样简单地使用javascript。否则,您可以使用插件,这有助于大型应用程序

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值=”;
变量日期=新日期();
开关(类型){
案件“日期”:
value=date.getDate();//以数字形式获取日期(1-31)
打破
案件“日期”:
value=date.getDay();//以数字形式获取工作日(0-6)
打破
“全年”案例:
value=date.getFullYear();//获取四位数的年份(yyyy)
打破
案例“小时数”:
value=date.getHours();//获取小时数(0-23)
打破
大小写“毫秒”:
value=date.getmillizes();//获取毫秒数(0-999)
打破
案例“会议记录”:
value=date.getMinutes();//获取分钟数(0-59)
打破
案例“月份”:
value=date.getMonth();//获取月份(0-11)
打破
案例“秒”:
value=date.getSeconds();//获取秒数(0-59)
打破
案例“时间”:
value=date.getTime();//获取时间(自1970年1月1日起的毫秒)
打破
}
$(元素)。同级('span')。文本(值);
}
li{
列表样式类型:无;
填充物:5px;
}
钮扣{
宽度:150px;
}
跨度{
左边距:100px;
}

  • 约会
  • 有一天
  • 整年
  • 上班时间
  • 获取毫秒数
  • 获得会议记录
  • 月薪
  • 获得秒数
  • 有时间

举个例子,你可以把它放在你想显示它的任何地方,而不需要参考页脚中的脚本或其他类似答案的地方

<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
newdate().getFullYear()>document.write(newdate().getFullYear());
以页脚上的版权说明为例

Copyright 2010 - <script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>
版权所有2010-new Date().getFullYear()>document.write(new Date().getFullYear());
TL;博士 这里找到的大多数答案都是正确的,仅当您需要基于您的本地机器的时区和偏移量(客户端)-来源的当前年份时,在大多数情况下,这些来源不能被认为是可靠的(因为它可能因机器而异)

可靠的来源是:

  • Web服务器的时钟(但请确保已更新)
  • 时间API和CDN
细节 在
Date
实例上调用的方法将返回一个基于计算机本地时间的值

更多详细信息请参见“MDN web文档”:

为方便起见,我从他们的文档中添加了一条相关注释:

(…)获取日期和时间或其组件的基本方法都在本地(即主机系统)时区和偏移中工作

另一个提到这一点的消息来源是:

重要的是要注意,如果某人的时钟关闭了几个小时,或者他们在不同的时区,那么Date对象将创建与您自己的计算机上创建的时间不同的时间

您可以使用的一些可靠来源包括:

  • 您的web服务器的时钟(首先检查它是否准确)
  • 时间API和CDN:
    • 其他相关API:

但是,如果您根本不关心时间准确性,或者您的用例需要一个相对于本地机器时间的时间值,那么您可以安全地使用Javascript的
Date
基本方法,如
Date.now()
,或
new Date().getFullYear()
(对于当前年份).

实例化类Date并调用其getFullYear方法以yyyy格式获取当前年份。 大概是这样的:

let currentYear = new Date().getFullYear;

currentYear变量将保存您要查找的值。

如果该值真的没有重复项,那就令人惊讶了。很接近,但不准确。请注意,地球上的每个人在同一时间的“当前”年份不一定相同。一个人必须考虑时区。这里的大多数答案都给出了用户所在时区的当前年份(假设代码在web浏览器中运行)。这是否回答了您的问题?所以每个人都要注意8760中的24小时,这不是真的!我叔叔不听劝告,掉了一个脚趾!对于同一系列中的其他函数,请参见:要根据世界时使用获取年份,请使用上面的页脚示例,如果它只是:答案:通过向上投票接受答案9次:)链接:
let currentYear = new Date().getFullYear;