在 JavaScript 中获取当前日期和时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10211145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting current date and time in JavaScript
提问by Ricardo
I have a script that prints the current date and time in JavaScript, but the DATE
is always wrong. Here is the code:
我有一个用 JavaScript 打印当前日期和时间的脚本,但DATE
总是错误的。这是代码:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDay() + "/" + currentdate.getMonth()
+ "/" + currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();
It should print 18/04/2012 15:07:33
and prints 3/3/2012 15:07:33
它应该打印18/04/2012 15:07:33
和打印3/3/2012 15:07:33
Any help? Thanks
有什么帮助吗?谢谢
回答by Mark Walters
.getMonth()
returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth()
in may will return 4
and not 5
.
.getMonth()
返回一个从零开始的数字,因此要获得正确的月份,您需要加 1,因此调用.getMonth()
可能会返回4
而不是5
。
So in your code we can use currentdate.getMonth()+1
to output the correct value. In addition:
所以在你的代码中,我们可以currentdate.getMonth()+1
用来输出正确的值。此外:
.getDate()
returns the day of the month <- this is the one you want.getDay()
is a separate method of theDate
object which will return an integer representing the current day of the week (0-6)0 == Sunday
etc
.getDate()
返回月份中的日期<- 这是您想要的日期.getDay()
是Date
对象的一个单独的方法,它将返回一个表示当前星期几 (0-6)0 == Sunday
等的整数
so your code should look like this:
所以你的代码应该是这样的:
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
JavaScript Date instances inherit from Date.prototype. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances
JavaScript Date 实例继承自 Date.prototype。您可以修改构造函数的原型对象以影响 JavaScript Date 实例继承的属性和方法
You can make use of the Date
prototype object to create a new method which will return today's date and time. These new methods or properties will be inherited by all instances of the Date
object thus making it especially useful if you need to re-use this functionality.
您可以使用Date
原型对象创建一个新方法,该方法将返回今天的日期和时间。这些新方法或属性将被Date
对象的所有实例继承,因此如果您需要重用此功能,则它特别有用。
// For todays date;
Date.prototype.today = function () {
return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}
// For the time now
Date.prototype.timeNow = function () {
return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}
You can then simply retrieve the date and time by doing the following:
然后,您可以通过执行以下操作简单地检索日期和时间:
var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();
Or call the method inline so it would simply be -
或者调用内联方法,所以它只是 -
var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();
回答by Chhorn Elit
To get time and date you should use
要获取您应该使用的时间和日期
new Date().toLocaleString();
>> "09/08/2014, 2:35:56 AM"
To get only the date you should use
只获取您应该使用的日期
new Date().toLocaleDateString();
>> "09/08/2014"
To get only the time you should use
只获得你应该使用的时间
new Date().toLocaleTimeString();
>> "2:35:56 AM"
Or if you just want the time in the format hh:mm
without AM/PM for US English
或者,如果您只想要hh:mm
美国英语中没有 AM/PM格式的时间
new Date().toLocaleTimeString('en-US', { hour12: false,
hour: "numeric",
minute: "numeric"});
>> "02:35"
or for British English
或英式英语
new Date().toLocaleTimeString('en-GB', { hour: "numeric",
minute: "numeric"});
>> "02:35"
Read more here.
在这里阅读更多。
回答by Daniel Lee
For this true mysql style use this function below: 2019/02/28 15:33:12
对于这种真正的 mysql 风格,请使用以下函数: 2019/02/28 15:33:12
- If you click the 'Run code snippet' button below
- It will show your an simple realtime digital clock example
- The demo will appear below the code snippet.
- 如果您单击下面的“运行代码段”按钮
- 它将显示一个简单的实时数字时钟示例
- 该演示将出现在代码片段下方。
function getDateTime() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
var day = now.getDate();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
if(month.toString().length == 1) {
month = '0'+month;
}
if(day.toString().length == 1) {
day = '0'+day;
}
if(hour.toString().length == 1) {
hour = '0'+hour;
}
if(minute.toString().length == 1) {
minute = '0'+minute;
}
if(second.toString().length == 1) {
second = '0'+second;
}
var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
return dateTime;
}
// example usage: realtime clock
setInterval(function(){
currentTime = getDateTime();
document.getElementById("digital-clock").innerHTML = currentTime;
}, 1000);
<div id="digital-clock"></div>
回答by Steve
Just use:
只需使用:
var d = new Date();
document.write(d.toLocaleString());
document.write("<br>");
回答by Chuck Norris
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"+(currentdate.getMonth()+1)
+ "/" + currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();
Change .getDay()
method to .GetDate()
and add one to month, because it counts months from 0.
将.getDay()
方法更改.GetDate()
为月份并将其加一,因为它从 0 开始计算月份。
回答by sp00m
This should do the trick:
这应该可以解决问题:
function dateToString(date) {
var month = date.getMonth() + 1;
var day = date.getDate();
var dateOfString = (("" + day).length < 2 ? "0" : "") + day + "/";
dateOfString += (("" + month).length < 2 ? "0" : "") + month + "/";
dateOfString += date.getFullYear();
return dateOfString;
}
var currentdate = new Date();
var datetime = "Last Sync: ";
datetime += dateToString(currentdate );
datetime += + currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
回答by Scorpion-Prince
You need to use getDate() to get the date part. The getDay() function returns the day number (Sunday = 0, Monday = 1...), and the getMonth() returns a 0 based index, so you need to increment it by 1.
您需要使用 getDate() 来获取日期部分。getDay() 函数返回天数(Sunday = 0,Monday = 1...),getMonth() 返回一个基于 0 的索引,因此您需要将其增加 1。
var currentdate = new Date();
var datetime = "Last Sync: " + currentdate.getDate() + "/"+ (parseInt(currentdate.getMonth()) + 1)
+ "/" + currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":" + currentdate.getSeconds();
回答by Rocket Hazmat
getDay()
gets the day of the week. 3
is Wednesday. You want getDate()
, that will return 18
.
getDay()
获取星期几。 3
是星期三。你想要的getDate()
,那会回来的18
。
Also getMonth()
starts at 0
, you need to add 1
to get 4
(April).
也是getMonth()
从 开始0
,你需要添加1
才能得到4
(四月)。
回答by JoyGuru
I have found the simplest way to get current date and time in JavaScript from here - How to get current Date and Time using JavaScript
我从这里找到了在 JavaScript 中获取当前日期和时间的最简单方法 - How to get current Date and Time using JavaScript
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var CurrentDateTime = date+' '+time;
回答by surendar
get current date and time
获取当前日期和时间
var now = new Date();
var datetime = now.getFullYear()+'/'+(now.getMonth()+1)+'/'+now.getDate();
datetime += ' '+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds();