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

后端 未结 9 794
青春惊慌失措
青春惊慌失措 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:24

    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.

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

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

    moment().hour();
    

    or

    moment().hours();
    

    without prior writing of any functions.

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

    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:

    var date = new Date();
    var current_hour = date.getHours();
    
    0 讨论(0)
  • 2020-12-23 13:30

    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;
    
    }
    
    0 讨论(0)
  • 2020-12-23 13:34

    To start your node in PST time zone , use following command in 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.

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

     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.

     DateLibrary.getWeekNumber(new Date("2015-06-15"),
        {operationType:"Week_of_Year",
            startDayOfWeek:"Tuesday"}) // Output : 24
    

    Refer other functions to fulfill your custom date requirements.

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

    There's native method to work with date

    const date = new Date();
    let hours = date.getHours();
    
    0 讨论(0)
提交回复
热议问题