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 ?

2
  • 3
    MomentJS is your best friend when it comes to JS and date manipulation. momentjs.com Commented Jan 3, 2014 at 7:43
  • you have to do it by breaking day, month and year from date. Commented Jan 3, 2014 at 7:45

6 Answers 6

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");
Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

2

You can simply use

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

DEMO

Comments

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?

Comments

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");

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.