Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
var fromDate = new Date($("#Date_From").val());
                           var date = new Date(fromDate).toDateString("yyyy-MM-dd");


but it does not changes the date into required format which is yyyy-MM-dd
Posted
Updated 28-May-18 19:58pm
v2
Comments
Rajesh Anuhya 21-Jun-13 7:21am    
what is the date value after executing second statement
--RA

Hi,

I am not sure if you can pass a specific format to the toDateString.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FDate%2FtoDateString[^]

I would consider checking out a library called moment.js for parsing, validating, manipulating, and formatting dates:
http://momentjs.com/[^]
 
Share this answer
 
Obviously, you don't understand what is a variable of type/object Date.

A variable of type Date or an object Date contains a value representing a Date or DateTime. This is an internal format suited for comparing 2 Dates.
The internal format can be like yyyymmdd.
There is no format conversion when copying a Date to a Date.

There format conversion on when you convert a String to a Date or a Date to a String.

Trying to format a Date variable while copying to another Date variable is non sense.

I recommend to search for tutorials for more details.
 
Share this answer
 
function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}
 
Share this answer
 
JavaScript
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
document.write(curr_year + "-" + curr_month + "-" + curr_date);
 
Share this answer
 
v2

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