5

I have use the following code snippet to convert the Date object to string .

 var startDate = new Date();
 var result = Globalize.parseDate(startDate, "MM/DD/YYYY");

but it will return the null value. How to convert the Date object to string specific format ?

Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
Raja
  • 209
  • 1
  • 7
  • 16

6 Answers6

4

To know all possible ways, check this link out.

I've put all the DEMOS here...

STANDARD JS:

<script type="text/javascript">
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1; //Months start with zero
    var curr_year = d.getFullYear();
    document.write(curr_month + "/" + curr_date + "/" + curr_year);
</script>

MOMENT.JS:

Download here...

<script>    
    var a = moment([2010, 1, 14, 15, 25, 50, 125]);
    a.format("MM/DD/YYYY,");    
</script>

Don't want to download, simply add this line:

<script src="http://momentjs.com/downloads/moment.min.js"></script>

jQuery UI:

$.datepicker.formatDate('yy-mm-dd', new Date(2007, 1 - 1, 26));

IE:

var d1=new Date();
d1.toString('MM-dd-yyyy');

Globalize:

var startDate = new Date();
var result = Globalize.format(startDate, "MM/DD/YYYY");
Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
3

I assume that you are using Globalize.

What you should do is to format the date, not parse it.

var startDate = new Date();
var result = Globalize.format(startDate, "MM/DD/YYYY");
xdazz
  • 158,678
  • 38
  • 247
  • 274
2

You can simply use

var startDate = new Date();
alert((startDate .getMonth() + 1) + '/' + startDate .getDate() + '/' +  startDate .getFullYear());

DEMO

Satpal
  • 132,252
  • 13
  • 159
  • 168
2

Try this:

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_date + "-" + curr_month + "-" + curr_year);

Reference: Where can I find documentation on formatting a date in JavaScript?

Community
  • 1
  • 1
Wilson
  • 608
  • 5
  • 16
1

MomentJS has a very robust set of time formatting options, you can use them.

Below is the example how it will work in your case

moment(stateDate).format("MM/DD/YYYY");
A Paul
  • 8,113
  • 3
  • 31
  • 61
0

This function takes a date object as a parameter and returns a string in MM/DD/YYYY format :

function format(date) {
    return [
        ('0' + (date.getMonth() + 1)).slice(-2),
        ('0' + date.getDate()).slice(-2),
        date.getFullYear()
    ].join('/')
}

Usage example (gives today's date in MM/DD/YYYY format) :

format(new Date) // "01/03/2014"

You can easily change the resulting format with a few modifications :

function format(date) {
    return [
        date.getDate(),
        date.getMonth() + 1,
        ('' + date.getFullYear()).slice(-2)
    ].join('-')
}

Usage example (gives today's date in D-M-YY format) :

format(new Date) // "3-1-14"

Playing around

I've tuned the function a bit, this might be interesting for very basic needs :)

function format(date, format) {
    var i = 0, bit;
    if (!format) format = 'MM/DD/YYYY'; // default
    format = format.split(/([^DMY]+)/i);
    while (bit = format[i]) {
        switch (bit.charAt(0).toUpperCase()) {
            case 'D': format[i] = date.getDate(); break;
            case 'M': format[i] = date.getMonth() + 1; break;
            case 'Y': format[i] = date.getFullYear(); break;
        }
        if (bit.length === 2) {
            format[i] = ('0' + format[i]).slice(-2);
        }
        i += 2;
    }
    return format.join('');
}

Usage examples :

format(new Date)             // "01/03/2014" (default)
format(new Date, 'd/m/y')    // "3/1/2014"
format(new Date, 'D/M/Y')    // "3/1/2014"
format(new Date, 'DD-MM-YY') // "03-01-14"
format(new Date, 'M/YY')     // "1/14"
Community
  • 1
  • 1