Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,

I from month and date i want to create one date and that i want to compare with todays date in javasript ..

How can i achive that in javacript

C#
DateTime uploadDate = new DateTime(year, month, 1);
            DateTime now = System.DateTime.Now;
            DateTime todaysDate = new DateTime(now.Year, now.Month, 1);

            bool isEditable = (uploadDate < todaysDate) ? true : false;


currently i am doing comparison on this login in my code behind ..

What I have tried:

JavaScript
function(s,e){var user = cmbUser.GetValue(); " +
            "  var year = cmbYear.GetValue(); " +
            "  var month = cmbMonth.GetValue(); " +
            "  var wholesaler = cmbWholeSaler.GetValue(); " +
            "var today = new Date(); " +
             "Date now = today.getDate(); " +
             "var todaysDate = new Date(today.getFullYear(),today.getMonth()+1,1); " +
             "Date uploadDate = new Date(year,month,1); " +
            " if (!(user > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP036") + "');} " +
            " else if (!(year > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP054") + "');} " +
            " else if (!(month > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP092") + "');} " +
            " else if ((uploadDate < todaysDate)) { alertText('" + MessageChanger.GetErrorMessage("AP092") + "');} " +
            " else if (!(wholesaler > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP034" + ",Wholesaler") + "');} " +
            " else { cbView.PerformCallback('1'); cmbDivision.SetEnabled(false); cmbUser.SetEnabled(false); cmbYear.SetEnabled(false);" +
            "cmbMonth.SetEnabled(false); cmbWholeSaler.SetEnabled(false);} }
Posted
Updated 23-Mar-16 2:31am
v2

1 solution

The code you have just compares the numeric day of the month; a generally accepted response is to convert the date to filetime and run the comparison that way. Without a day in the Date constructor, it will default to the first day of the month. Also, the month in the constructor is zero-indexed, so Jan will be 0, Dec will be 11.

You can also de-clutter the code quite a bit using this mechanism.

JavaScript
var user = cmbUser.GetValue();
var wholesaler = cmbWholeSaler.GetValue();
var today = new Date().getTime();
var uploadDate = new Date(cmbYear.GetValue(),cmbMonth.GetValue() - 1).getTime();


If cmbMonth is also zero-indexed, remove the "-1".

You also need to modify your checks, since month CAN be 0. If you are doing null checking (and it looks like you are), you can simplify this check with:
JavaScript
else if (!month) { alertText('" + MessageChanger.GetErrorMessage("AP092") + "');} 
 
Share this answer
 
Comments
Torakami 25-Mar-16 1:48am    
Hi ,

I have tried as per your own way ,, but this is not working

string uploadClick = "function(s,e){var user = cmbUser.GetValue(); " +
" var year = cmbYear.GetValue(); " +
" var month = cmbMonth.GetValue(); " +
" var wholesaler = cmbWholeSaler.GetValue(); " +
" var today = new Date().getTime(); " +
" var uploadDate = new Date(cmbYear.GetValue(),cmbMonth.GetValue() - 1,1).getTime(); " +
" if (!(user > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP036") + "');} " +
" else if (!(year > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP054") + "');} " +
" else if (!(month > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP092") + "');} " +
" else if (!(wholesaler > 0)) { alertText('" + MessageChanger.GetErrorMessage("AP034" + ",Wholesaler") + "');} " +
" else if ((uploadDate > today)) { alertText('can not upload for current or future months');} " +
" else { cbView.PerformCallback('1'); cmbDivision.SetEnabled(false); cmbUser.SetEnabled(false); cmbYear.SetEnabled(false);" +
"cmbMonth.SetEnabled(false); cmbWholeSaler.SetEnabled(false);} }";
Nathan Minier 25-Mar-16 7:00am    
Okay, how is it failing then?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900