How to get server time with node.js

0

The way I do it with javascript on the client side is the following

function srvTime(){
     try {

         xmlHttp = new XMLHttpRequest();
     }
     catch (err1) {
         //IE
         try {
             xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
         }
         catch (err2) {
             try {
                 xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
             }
             catch (eerr3) {
                 alert("AJAX not supported");
             }
         }
     }
     xmlHttp.open('HEAD',window.location.href.toString(),false);
     xmlHttp.setRequestHeader("Content-Type", "text/html");
     xmlHttp.send('');
     return xmlHttp.getResponseHeader("Date");
 }

However with node.js I can not since the XMLHttpRequest library does not detect me and marks window as undefined.

How could I do it with node

Note: I need the server I'm connected to, not the one on the device.

    
asked by Danitza Ayala 22.09.2018 в 01:38
source

2 answers

1

To get the server time with node, simply place:

var fecha= new Date();
var hora_actual = fecha.getHours();

or

var fecha = Date.now();
    
answered by 22.09.2018 в 03:37
0

I recommend you use the moment dependency, which allows you to manage dates and times in a very simple way.

To incorporate moment into your project, you must execute the npm i --save moment command.

Example:

const moment = require('moment')

let currentDate = moment().format('YYYY-MM-DD')

let currentTime = moment().format('hh:mm:ss')
    
answered by 06.01.2019 в 09:58