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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kdotkannanCommented:

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 PlungjanIT 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.