Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the time of day in javascript/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.

like image 269
TIMEX Avatar asked Sep 09 '11 06:09

TIMEX


People also ask

How do I get current UTC time in node JS?

Use the toUTCString() method to get the current date and time in utc, e.g. new Date(). toUTCString() .


1 Answers

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.

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;  } 
like image 193
Ionică Bizău Avatar answered Oct 09 '22 20:10

Ionică Bizău