-2

I have two date fields: FROM and TO. I am trying to pass the date to JavaScript so that I can compare the dates and month of the two dates entered.

I am getting the date using

var date= document.getElementById("fromdate").value;

Here I am getting the date from an element in the form of yyyy-mm-dd. Now I am trying to extract the date, month and year individually for some comparison.

I am using var month=date.getMonth(); but it's not working.

rnevius
  • 26,578
  • 10
  • 58
  • 86
anish
  • 97
  • 6
  • 12
  • The value is a String, so calling Date methods on it isn't going to work. You can get the parts as an array using `var parts = value.split('-')`. – RobG Jul 09 '15 at 05:35
  • There is no date object in the code you shared. You need to either convert the string into a date object, or split the string into the parts you need. – rnevius Jul 09 '15 at 05:37
  • Ok guys splitting it out will be better :) – anish Jul 09 '15 at 05:38

3 Answers3

1

This isn't working because you can't get data directly from a string, you first must create a new Date object using your string, then you'll be able to manipulate it.

var myDate = new Date(document.getElementById("fromdate").value);
myDate.getMonth();

Also note that your yyyy-mm-dd format may not work for all browsers since all of them interpret javascript differently (It does work in chrome though). Just something to be mindful of.

Victor Johnson
  • 416
  • 2
  • 7
  • let me try it victor – anish Jul 09 '15 at 05:41
  • Please don't suggest parsing strings with the Date constructor. It is unreliable and will remain so at least until ECMA-262 ed 6 is ubiquitous (it isn't yet), and even then only for certain ISO formats. – RobG Jul 09 '15 at 05:45
  • @RobG, how else would you manipulate dates without pulling in a library like moment.js? Suppose somebody wanted to take a date in string format and find the date 100 days from now... I don't see a more reliable solution to problems like that. – Victor Johnson Jul 09 '15 at 05:48
  • @VictorJohnson—whether conversion to a Date or just using the string parts is more appropriate is up to the OP. The question was for the parts. To parse the OP format is 2 lines of code (which includes validating the date), I find it difficult to justify a library for such a simple task. – RobG Jul 09 '15 at 06:29
0

create a date object using the string..

var date= document.getElementById("fromdate").value;
var obj=new Date(date);

and use

obj.getFullYear(); 

to get Year....

use

obj.getMonth();

to get Month.

use

obj.getDate();

to get day..

Note : note that your yyyy-mm-dd format may not work for all browsers every browser interpret js differently..

so its' better to split and use..

like

var date= document.getElementById("fromdate").value;
var res=date.split('-');

res[0] contains Year. res[1] contains Month. res[2] contains Date.

kavetiraviteja
  • 2,058
  • 1
  • 15
  • 35
0

function splitDate(date){
  var result = date.split('-');
  return result;
  
}

var splittedDate = splitDate('2015-07-09');
var year = splittedDate[0];
var month = splittedDate[1];
var day = splittedDate[2];
alert(year);
alert(month);
alert(day);
Bibek Sharma
  • 3,210
  • 6
  • 42
  • 64