Home >Web Front-end >JS Tutorial >How to Convert a Date to MM/dd/yyyy Format in JavaScript?
How to Convert a Date to MM/dd/yyyy Format in JavaScript
When working with dates in JavaScript or jQuery, it's often necessary to format them in a specific way, such as MM/dd/yyyy.
Problem:
You have a date string in the format '2010-10-11T00:00:00 05:30' and you need to convert it to MM/dd/yyyy.
Solution:
To convert the date string to MM/dd/yyyy format, use the following steps:
Create a new Date object from the date string.
<code class="js">var date = new Date('2010-10-11T00:00:00+05:30');</code>
Concatenate the formatted date components into a single string in MM/dd/yyyy format.
<code class="js">alert(((date.getMonth() + 1) > 9) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear());</code>
This code will output the formatted date in MM/dd/yyyy format.
The above is the detailed content of How to Convert a Date to MM/dd/yyyy Format in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!