6

I have 4 INPUT fields for month, day, hour, minute.

<input type="text" id="month" name="month" value="" />
   <input type="text" id="day" name="day" value="" />
  <input type="text" id="hour" name="hour" value="" />
  <input type="text" id="minute" name="minute" value="" />

I am trying to get CURRENT time using JS and insert into each field using jQuery.

Can you help me to work out the right code to accomplish this?

4 Answers 4

9
$(document).ready(function() {
   var now = new Date();
   $("#month").val(now.getMonth() + 1); //Months in JS start from 0
   $("#day").val(now.getDate());
   $("#hour").val(now.getHours());
   $("#minute").val(now.getMinutes());
});

See this MDN page for more information on the Date object.

Sign up to request clarification or add additional context in comments.

3 Comments

now.getDate() should probably be now.getDay()+1 ;)
No it shouldn't. getDay returns the current day of the week (e.g. 4 currently). getDate returns the day of the month (e.g. 11 currently).
@jonny pixel - There should be a button to the left of my answer so you can mark it as the accepted answer. Glad I could help :)
3

meh... for what it's worth: http://jsfiddle.net/V3J8c/1

$("#month").val(months[new Date().getMonth()])
$("#day").val(new Date().getDate())
$("#hour").val(new Date().getHours())
$("#minute").val(new Date().getMinutes())

2 Comments

Thanks for sharing the link. Nice tool. Really nice.
@jonnypixel You're quite welcome! I absolutely love this tool, and i learned about it from SO too.
0
var date = new Date();
$('#month').val(date.getMonth() + 1);
$('#day').val(date.getDate());
$('#hour').val(date.getHours()); // This will be based on a 24-hour clock.
$('#minute').val(date.getMinutes());

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

Comments

0
var date = new Date(),
    currDay = date.getDate(),
    currHours = date.getHours(),
    currMonths = date.getMonth() + 1;
    currMinutes = date.getMinutes();

$("#day").val(currDay);
$("#month").val(currMonths);
$("#minute").val(currMinutes);
$("#hour").val(currHours);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.