In c# it's enough to do:
new DateTime(year, month, day);
How should I do in JS?
Is the following code correct?
var birthYear = parseInt($("#BirthYear").valueOf());
var birthMonth = parseInt($("#BirthMonth").valueOf());
var birthDay = parseInt($("#BirthYear").valueOf());
var birthDate = new Date(birthYear, birthMonth + 1, birthDay);
In c# it's enough to do:
new DateTime(year, month, day);
How should I do in JS?
Is the following code correct?
var birthYear = parseInt($("#BirthYear").valueOf());
var birthMonth = parseInt($("#BirthMonth").valueOf());
var birthDay = parseInt($("#BirthYear").valueOf());
var birthDate = new Date(birthYear, birthMonth + 1, birthDay);
Share
Improve this question
edited Oct 14, 2014 at 14:54
Revious
asked Oct 14, 2014 at 14:45
ReviousRevious
8,18633 gold badges102 silver badges158 bronze badges
10
-
3
var DateTime = new Date(year, month, day, hours, minutes, seconds, milliseconds);
? – briosheje Commented Oct 14, 2014 at 14:46 - Yes, I couldnt find this question here.. if it's not a dup let's answer and I will accept! – Revious Commented Oct 14, 2014 at 14:48
- 1 There are already some answer there Revious, I'm sorry but I don't think I deserve any reputation for such a basic question that can be answered through google or whatever kind of thing. The only important thing is that you got it ;) – briosheje Commented Oct 14, 2014 at 14:49
- 3 Everyone wrote something correct down there, just choose the answer you prefere. The only important thing, again, is that you got it, regardless how precise the answer is, for such a basic task just check how simile is the javascript solution pared to the C# one ;). In any case yes, month January is 0, not that obvious indeed :). For more (VERY) precise infos, check the official MDN: developer.mozilla/en-US/docs/Web/JavaScript/Reference/… .. About the month, it is said that: prntscr./4w3qj9 <-- "from 0 to 11" – briosheje Commented Oct 14, 2014 at 14:52
-
2
Re your edit: you need
birthMonth - 1
(assuming 1 <= birthMonth <= 12). – 001 Commented Oct 14, 2014 at 15:04
3 Answers
Reset to default 7var whateverName = new Date(year, month, day);
Month is 0 indexed, so you need to put in 0 if you want January etc.
You can also enter negative months and dates. This will "count backwards" that many months or days.
This allows you to get the last date of a month by entering the month ahead of it and then 0 as the day.
var dateObject = new Date();
dateObject.getFullYear() + " " + dateObject.getMonth() + 1 + " " + dateObject.getDate();
JavaScript Dates
new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)