如何在 javascript/Node.js 中获取一天中的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7357734/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:57:47  来源:igfitidea点击:

How do I get the time of day in javascript/Node.js?

javascriptdatetimedatenode.js

提问by TIMEX

I want to get 1 to 24, 1 being 1am Pacific Time.

我想得到1 到 24, 1 是太平洋时间凌晨 1 点。

How can I get that number in Node.JS?

我怎样才能在 Node.JS 中获得那个数字?

I want to know what time it is in Pacific time right now.

我想知道现在太平洋时间是几点。

回答by Ionic? Biz?u

This function will return you the date and time in the following format: YYYY:MM:DD:HH:MM:SS. It also works in Node.js.

此函数将按以下格式返回日期和时间:YYYY:MM:DD:HH:MM:SS. 它也适用于 Node.js。

function getDateTime() {

    var date = new Date();

    var hour = date.getHours();
    hour = (hour < 10 ? "0" : "") + hour;

    var min  = date.getMinutes();
    min = (min < 10 ? "0" : "") + min;

    var sec  = date.getSeconds();
    sec = (sec < 10 ? "0" : "") + sec;

    var year = date.getFullYear();

    var month = date.getMonth() + 1;
    month = (month < 10 ? "0" : "") + month;

    var day  = date.getDate();
    day = (day < 10 ? "0" : "") + day;

    return year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec;

}

回答by jtsao22

You should checkout the Date object.

您应该签出Date 对象

In particular, you can look at the getHours()method for the Date object.

特别是,您可以查看Date 对象的getHours()方法。

getHours() return the time from 0 - 23, so make sure to deal with it accordingly. I think 0-23 is a bit more intuitive since military time runs from 0 - 23, but it's up to you.

getHours() 返回 0 - 23 之间的时间,因此请确保相应地处理它。我认为 0-23 更直观一些,因为军用时间从 0 到 23,但这取决于您。

With that in mind, the code would be something along the lines of:

考虑到这一点,代码将类似于以下内容:

var date = new Date();
var current_hour = date.getHours();

回答by user2513149

Check out the moment.jslibrary. It works with browsers as well as with Node.JS. Allows you to write

moment.js图书馆看看。它适用于浏览器以及 Node.JS。允许你写

moment().hour();

or

或者

moment().hours();

without prior writing of any functions.

无需事先编写任何函数。

回答by ghchinoy

Both prior answers are definitely good solutions. If you're amenable to a library, I like moment.js- it does a lot more than just getting/formatting the date.

两个先前的答案绝对是很好的解决方案。如果你喜欢一个库,我喜欢moment.js- 它不仅仅是获取/格式化日期。

回答by p2lem8dev

There's native method to work with date

有使用日期的本地方法

const date = new Date();
let hours = date.getHours();

回答by Pablo Andres Diaz Mazzaro

If you only want the time string you can use this expression (with a simple RegEx):

如果您只想要时间字符串,您可以使用此表达式(使用简单的 RegEx):

new Date().toISOString().match(/(\d{2}:){2}\d{2}/)[0]
// "23:00:59"

回答by Josh

var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var day  = date.getDate();
day = (hour > 12 ? "" : "") + day - 1;
day = (day < 10 ? "0" : "") + day;
x = ":"
console.log( month + x + day + x + year )

It will display the date in the month, day, then the year

它将显示月、日、年中的日期

回答by Vikash Rajpurohit

To start your node in PST time zone , use following command in ubuntu.

要在 PST 时区启动您的节点,请在 ubuntu 中使用以下命令。

TZ=\"/usr/share/zoneinfo/GMT+0\" && export TZ && npm start &

Then You can refer Date Libraryto get the custom calculation date and time functions in node.

然后您可以参考日期库来获取节点中的自定义计算日期和时间函数。

To use it client side refer this link, download index.js and assertHelper.js and include that in your HTML.

要在客户端使用它,请参考此链接,下载 index.js 和 assertHelper.js 并将其包含在您的 HTML 中。

<script src="assertHelper.js"></script>
<script type="text/javascript" src="index.js"></script>
$( document ).ready(function() {
    DateLibrary.getDayOfWeek(new Date("2015-06-15"),{operationType:"Day_of_Week"}); // Output : Monday
}

You can use different functions as given in examples to get custom dates.

您可以使用示例中给出的不同函数来获取自定义日期。

If first day of week is Sunday, what day will be on 15th June 2015.

如果一周的第一天是星期日,那么 2015 年 6 月 15 日是哪一天。

 DateLibrary.getDayOfWeek(new Date("2015-06-15"),
    {operationType:"Day_Number_of_Week",
        startDayOfWeek:"Sunday"}) // Output : 1

If first day of week is Tuesday, what week number in year will be follow in 15th June 2015 as one of the date.

如果一周的第一天是星期二,那么 2015 年 6 月 15 日将跟随一年中的哪个周数作为日期之一。

 DateLibrary.getWeekNumber(new Date("2015-06-15"),
    {operationType:"Week_of_Year",
        startDayOfWeek:"Tuesday"}) // Output : 24

Refer other functions to fulfill your custom date requirements.

参考其他函数来满足您的自定义日期要求。