Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

javascript - Display default tomorrow date and time as 8Am in date field

Hi all i am trying to display tomorrow date and time(8am) in my date field. as i have tried some code it is showing only tomorrow date but not time can any one help me how to show time 8am as by default

Here is my code:

     <div class="col-xs-6 date" style="display:none;">
                            <input class="form-control" type="date" id="inputDate"/>
                        </div>

    <script>
    // Declare variables
    var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
    // Set values
    $("#inputDate").val(getFormattedDate(tomorrow));
    // Get date formatted as YYYY-MM-DD
    function getFormattedDate (date) {
        return date.getFullYear()
            + "-"
            + ("0" + (date.getMonth() + 1)).slice(-2)
            + "-"
            + ("0" + date.getDate()).slice(-2);
    }
</script>

Can anyone help me how can i display time as by default 8 am and later if user want to change they can change their date and time according to their requirement.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First you need to change your input type to datetime-local. So that it will support for date & time. Then use the moment to format date.

$("#inputDate").val(moment().add(1, 'days').hours(8).startOf('hour').format("YYYY-MM-DDTHH:mm:ss"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<div class="col-xs-6 date" style="display:block;">
  <input class="form-control" type="datetime-local" id="inputDate" value="" />
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...