Format date to MM/dd/yyyy in JavaScript
Try this; bear in mind that JavaScript months are 0-indexed, whilst days are 1-indexed.
var date = new Date('2010-10-11T00:00:00+05:30'); alert(((date.getMonth() > 8) ? (date.getMonth() + 1) : ('0' + (date.getMonth() + 1))) + '/' + ((date.getDate() > 9) ? date.getDate() : ('0' + date.getDate())) + '/' + date.getFullYear());
How to format JavaScript date to mm/dd/yyyy?
var date = new Date('Mon Aug 14 2017 00:00:00 GMT-0500 (CDT)');var newdate= (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Format JavaScript date as yyyy-mm-dd
You can do:
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('-');
}
console.log(formatDate('Sun May 11,2014'));
How to get current formatted date dd/mm/yyyy in Javascript and append it to an input
I hope this is what you want:
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = dd + '/' + mm + '/' + yyyy;
document.getElementById('DATE').value = formattedToday;
How do I get the current date in JavaScript?
Get current date in DD-Mon-YYY format in JavaScript/Jquery
There is no native format in javascript for DD-Mon-YYYY
.
You will have to put it all together manually.
The answer is inspired from :
How do I format a date in JavaScript?
// Attaching a new function toShortFormat() to any instance of Date() class
Date.prototype.toShortFormat = function() {
const monthNames = ["Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];
const day = this.getDate();
const monthIndex = this.getMonth();
const monthName = monthNames[monthIndex];
const year = this.getFullYear();
return `${day}-${monthName}-${year}`;
}
// Now any Date object can be declared
let anyDate = new Date(1528578000000);
// and it can represent itself in the custom format defined above.
console.log(anyDate.toShortFormat()); // 10-Jun-2018
let today = new Date();
console.log(today.toShortFormat()); // today's date
Convert date object in dd/mm/yyyy hh:mm:ss format
You can fully format the string as mentioned in other posts. But I think your better off using the locale functions in the date object?
var d = new Date("2017-03-16T17:46:53.677"); console.log( d.toLocaleString() );
Related Topics
Difference Between "Change" and "Input" Event for an 'Input' Element
How to Execute Promises Sequentially, Passing the Parameters from an Array
How to Post Form Data with Fetch API
"Object Doesn't Support This Property or Method" Ie10/11
Adding Console.Log to Every Function Automatically
Tool to Unminify/Decompress JavaScript
JavaScript Url Decode Function
How to Scroll to Specific Item Using Jquery
How to Create Dynamic Variable Names Inside a Loop
How to Do Outerhtml in Firefox
How to Set Time Delay in JavaScript
Typeerror: Console.Log(...) Is Not a Function
Differencebetween Bower and Npm
How to Link Each User to Their Data in Firebase