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

时间:2021-04-24 07:31:41

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.

我想知道现在太平洋时间是什么时候。

6 个解决方案

#1


114  

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();

#2


123  

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.

此功能将以以下格式返回日期和时间:yyyyy: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;

}

#3


13  

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

查看。js库。它不仅适用于Node.JS,也适用于浏览器。允许您编写

moment().hour();

or

moment().hours();

without prior writing of any functions.

不写任何函数。

#4


12  

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.

之前的答案都是好的解决方案。如果你愿意去图书馆,我喜欢时间。它所做的不仅仅是获取/格式化日期。

#5


2  

There's a method easier to write and do same function.

有一种方法更容易编写和执行相同的功能。

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

It's easy to use:

易于使用:

var hours = (new Date()).getHours();

var hours = (new Date() .getHours();

#6


0  

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 Library to 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.

要使用it客户端参考此链接,请下载索引。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.

引用其他函数来满足您的自定义日期需求。

#1


114  

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();

#2


123  

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.

此功能将以以下格式返回日期和时间:yyyyy: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;

}

#3


13  

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

查看。js库。它不仅适用于Node.JS,也适用于浏览器。允许您编写

moment().hour();

or

moment().hours();

without prior writing of any functions.

不写任何函数。

#4


12  

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.

之前的答案都是好的解决方案。如果你愿意去图书馆,我喜欢时间。它所做的不仅仅是获取/格式化日期。

#5


2  

There's a method easier to write and do same function.

有一种方法更容易编写和执行相同的功能。

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

It's easy to use:

易于使用:

var hours = (new Date()).getHours();

var hours = (new Date() .getHours();

#6


0  

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 Library to 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.

要使用it客户端参考此链接,请下载索引。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.

引用其他函数来满足您的自定义日期需求。