Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date to String object conversion

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 ?

like image 592
Raja Avatar asked Dec 20 '22 19:12

Raja


2 Answers

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");
like image 159
Anshu Dwibhashi Avatar answered Jan 06 '23 20:01

Anshu Dwibhashi


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");
like image 42
xdazz Avatar answered Jan 06 '23 20:01

xdazz