如何在 javascript/Node.js 中获取一天中的时间?
我想要获取1 到 24,1 表示太平洋时间凌晨 1 点。
如何在 Node.JS 中获取该数字?
我想知道现在太平洋时间是什么时间。
I want to get 1 to 24, 1 being 1am Pacific Time.
How can I get that number in Node.JS?
I want to know what time it is in Pacific time right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
此函数将以以下格式返回日期和时间:
YYYY:MM:DD:HH:MM:SS
。它也适用于 Node.js。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.您应该检查日期对象。
特别是,您可以查看 Date 对象的 getHours() 方法。
getHours() 返回 0 - 23 之间的时间,因此请确保相应地处理它。我认为 0-23 更直观一些,因为军事时间是从 0 - 23 开始的,但这取决于你。
考虑到这一点,代码将类似于:
You should checkout the Date object.
In particular, you can look at the getHours() method for the Date object.
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.
With that in mind, the code would be something along the lines of:
查看
moment.js
库。它适用于浏览器以及 Node.JS。允许您编写或
无需事先编写任何函数。
Check out the
moment.js
library. It works with browsers as well as with Node.JS. Allows you to writeor
without prior writing of any functions.
前面的两个答案绝对是很好的解决方案。如果您愿意使用图书馆,我喜欢 moment.js - 它的作用不仅仅是获取/格式化日期。
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.
有处理日期的本机方法
There's native method to work with date
如果您只需要时间字符串,可以使用此表达式(使用简单的正则表达式):
If you only want the time string you can use this expression (with a simple RegEx):
它将显示日期,依次为月、日、年
It will display the date in the month, day, then the year
就我而言,我将其用作全局变量,然后调用它来检查运行时间。
下面是我的index.js
从另一个文件调用全局值
For my instance i used it as a global and then called its for a time check of when hours of operation.
Below is from my index.js
To call the global's value from another file
要在 PST 时区启动节点,请在 ubuntu 中使用以下命令。
然后你可以参考日期库来获取node中自定义的计算日期和时间函数。
要在客户端使用它,请参阅此链接,下载index.js和assertHelper.js并包含它在你的 HTML 中。
您可以使用示例中给出的不同函数来获取自定义日期。
如果一周的第一天是星期日,则 2015 年 6 月 15 日是哪一天。
如果一周的第一天是星期二,则星期几年将是
接下来的日期之一是 2015 年 6 月 15 日。
请参考其他函数来满足您的自定义日期要求。
To start your node in PST time zone , use following command in ubuntu.
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.
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.
If first day of week is Tuesday, what week number in year will be
follow in 15th June 2015 as one of the date.
Refer other functions to fulfill your custom date requirements.