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

后端 未结 9 795
青春惊慌失措
青春惊慌失措 2020-12-23 13:23

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

相关标签:
9条回答
  • 2020-12-23 13:41

    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

    global.globalDay = new Date().getDay(); 
    
    global.globalHours = new Date().getHours();
    

    To call the global's value from another file

        /*
         Days of the Week.
         Sunday = 0, Monday = 1, Tuesday = 2, Wensday = 3, Thursday = 4, Friday = 5, Saturday = 6
    
         Hours of the Day.
         0 = Midnight, 1 = 1am, 12 = Noon, 18 = 6pm, 23 = 11pm
         */
    
    if (global.globalDay === 6 || global.globalDay === 0) {
         console.log('Its the weekend.');
    } else if (global.globalDay > 0 && global.globalDay < 6 && global.globalHours > 8 && global.globalHours < 18) {
         console.log('During Business Hours!');
    } else {
         console.log("Outside of Business hours!");
    }
    
    0 讨论(0)
  • 2020-12-23 13:44
    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

    0 讨论(0)
  • 2020-12-23 13:48

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

    new Date().toISOString().match(/(\d{2}:){2}\d{2}/)[0]
    // "23:00:59"
    
    0 讨论(0)
提交回复
热议问题