0

I have my dates stored as a string values in variables sDate and eDate. I have to compare them in java script. Before I compare the dates I have to define them as Date objects. I tried to use this:

var jsObj = {};
<cfoutput query="getQuery">
jsObj["#year#_#Id#"].startdate = "#DateFormat(sDate,'mm/dd/yyyy')#";
jsObj["#year#_#Id#"].enddate = "#DateFormat(eDate,'mm/dd/yyyy')#";
</cfoutput>

var sDate = new Date('mm/dd/yyyy');
var eDate = new Date('mm/dd/yyyy');

if(aartemp[1] == bldg){
    if(jsObj[key].startdate == sDate || jsObj[key].enddate == eDate){
        alert('Error!');
    }
}

I got an error when I tried to print value sDate that says is not defined. If you know what I should change please let me know.

espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

2 Answers2

1

You have to replace the characters by digits, representing a real date in new Date('mm/dd/yyyy'), e.g. new Date('05/05/2015').

Pavel Gatnar
  • 3,987
  • 2
  • 19
  • 29
0

When you call the Date() constructor, you need to pass it a value to make into the date - not a template for formatting the date. You can either make a Date object by calling Date(), which gives you the current date/time, or you need to supply some value(s) to create the date. Here are some examples from MDN page for the Date object:

var today = new Date();
var birthday = new Date('December 17, 1995 03:24:00');
var birthday = new Date('1995-12-17T03:24:00');
var birthday = new Date(1995, 11, 17);
var birthday = new Date(1995, 11, 17, 3, 24, 0);

if "#DateFormat(sDate,'mm/dd/yyyy')#" outputs a date in 'mm/dd/yyyy' format, you could use that to initialize your dates:

<cfoutput query="getQuery">
jsObj["#year#_#Id#"].startdate = "#DateFormat(sDate,'mm/dd/yyyy')#";
jsObj["#year#_#Id#"].enddate = "#DateFormat(eDate,'mm/dd/yyyy')#";
</cfoutput>

var sDate = new Date(jsObj["#year#_#Id#"].startdate);
var eDate = new Date(jsObj["#year#_#Id#"].enddate);
Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61