How do I use yyyy-mm-dd format in javascript?

I'm a beginner at javascript. I need to figure out how to convert my date to be yyyy-mm-dd format and not the "dateformat" "yyyy/mm/dd" i'm getting now. "+DayOfMonth+" which = mm/dd/yyyy

Thanks,

B
<script language="JavaScript">
<!--
 
// function to populate the date on the form and to close this window. --->
function ShowDate(DayOfMonth) {
    var FormName="<cfoutput>#URL.FormName#</cfoutput>";
    var FieldName="<cfoutput>#URL.FieldName#</cfoutput>";
    eval("self.opener.document." + FormName + "." + FieldName + ".value='"+DayOfMonth+"'");
    window.close();
}
 
//-->
</script>

Open in new window

brihol44Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
hieloConnect With a Mentor Commented:
There is no built-in formatter. You would need to do this yourself. I implemented this for you on the code below.
<script language="JavaScript">
<!--
 
// function to populate the date on the form and to close this window. --->
function ShowDate(DayOfMonth) {
    var FormName="<cfoutput>#URL.FormName#</cfoutput>";
    var FieldName="<cfoutput>#URL.FieldName#</cfoutput>";
    var t = DayOfMonth.split("/");
    DayOfMonth=t[2]+"-"+t[1]+"-"+t[0];
    eval("self.opener.document." + FormName + "." + FieldName + ".value='"+DayOfMonth+"'");
    window.close();
}
 
//-->
</script>

Open in new window

0
 
kdotkannanConnect With a Mentor Commented:

Try this code snippet  given below. You can change the format by modifying the function getDate().
With this method, you need not bother about the string format of "DayOfMonth". You can call the function like ShowDate() instead of ShowDate(DayOfMonth) ..

For your convenience I have added the whole code, so that you can understand how it can be done.
.. kannan..
<script language="JavaScript">
<!--
function getDate() {
		var thisTime = new Date();
		var va = new String("");
			va = thisTime.getFullYear()  + "-" +  thisTime.getMonth() + 1  + "-" + thisTime.getDate()   ;
			return (va) ;
}
 
 
// function to populate the date on the form and to close this window. --->
function ShowDate(DayOfMonth) {
 // DayOfMonth is not needed now
    var FormName="<cfoutput>#URL.FormName#</cfoutput>";
    var FieldName="<cfoutput>#URL.FieldName#</cfoutput>";
    eval("self.opener.document." + FormName + "." + FieldName + ".value='"+getDate()+"'");
    window.close();
}
 
//-->
</script>

Open in new window

0
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
@kdotkannan:That is assuming he want's todays date.

@B: here is a simpler (and safer) way to put the date in the field:

window.opener.document.forms[FormName].elements[FieldName].value=DayOfMonth;
0
All Courses

From novice to tech pro — start learning today.