Date time format

Use the code below to increment the time by 3 hours to get the new time.

If the date created is of the format 2023-07-21 11:56:27, then the time changes to 2023-07-21 14:56:27

If the date created is 2023-07-21 22:56:27, time changes to 2023-07-22 01:56:27

var inputDateTimeString = <get date created>;
            var inputDateTime = new Date(inputDateTimeString);
            inputDateTime.setHours(inputDateTime.getHours() + 3);
            var year = inputDateTime.getFullYear();
            var month = String(inputDateTime.getMonth() + 1).padStart(2, '0');
            var date = String(inputDateTime.getDate()).padStart(2, '0');
            var hours = String(inputDateTime.getHours()).padStart(2, '0');
            var minutes = String(inputDateTime.getMinutes()).padStart(2, '0');
            var seconds = String(inputDateTime.getSeconds()).padStart(2, '0');
            var newDateTimeString = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
            log.debug("date created", newDateTimeString);

Leave a comment

Your email address will not be published. Required fields are marked *