How to Find Documentation on Formatting a Date in JavaScript

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

I love 10 ways to format time and date using JavaScript and Working with Dates.

Basically, you have three methods and you have to combine the strings for yourself:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year

Example:

var d = new Date();var curr_date = d.getDate();var curr_month = d.getMonth() + 1; //Months are zero basedvar curr_year = d.getFullYear();console.log(curr_date + "-" + curr_month + "-" + curr_year);

How do I format a date in JavaScript?

For custom-delimited date formats, you have to pull out the date (or time)
components from a DateTimeFormat object (which is part of the
ECMAScript Internationalization API), and then manually create a string
with the delimiters you want.

To do this, you can use DateTimeFormat#formatToParts. You could
destructure the array, but that is not ideal, as the array output depends on the
locale:

{ // example 1
let f = new Intl.DateTimeFormat('en');
let a = f.formatToParts();
console.log(a);
}
{ // example 2
let f = new Intl.DateTimeFormat('hi');
let a = f.formatToParts();
console.log(a);
}

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

I love 10 ways to format time and date using JavaScript and Working with Dates.

Basically, you have three methods and you have to combine the strings for yourself:

getDate() // Returns the date
getMonth() // Returns the month
getFullYear() // Returns the year

Example:

var d = new Date();var curr_date = d.getDate();var curr_month = d.getMonth() + 1; //Months are zero basedvar curr_year = d.getFullYear();console.log(curr_date + "-" + curr_month + "-" + curr_year);

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 do I format a date in JavaScript?

For custom-delimited date formats, you have to pull out the date (or time)
components from a DateTimeFormat object (which is part of the
ECMAScript Internationalization API), and then manually create a string
with the delimiters you want.

To do this, you can use DateTimeFormat#formatToParts. You could
destructure the array, but that is not ideal, as the array output depends on the
locale:

{ // example 1
let f = new Intl.DateTimeFormat('en');
let a = f.formatToParts();
console.log(a);
}
{ // example 2
let f = new Intl.DateTimeFormat('hi');
let a = f.formatToParts();
console.log(a);
}

How to change a date format in JavaScript

Convert it do a Date, get the make a string out of the components.

The month needs to add 1 since for some reason, months are zero-indexed (January == 0, February == 1, ..., December == 11)

const dateString = "13 Dec,2021 12:59:58";

const getNewString = (dateString) => {
const date = new Date(dateString);
return `${date.getDate()}-${date.getMonth()+1}-${date.getFullYear()}`;
}

console.log(getNewString(dateString));

Format a date string in javascript

Use Moment.js and the .format function.

moment('2017-06-10T16:08:00').format('MM/DD/YYYY');

Will output

06/10/2017

Beside the format function Moment.js will enrich you will alot more useful functions.

How to change the format of a date in JavaScript?

Your date 2018-06-13T13:28:14+0000 is in UTC format.

One of the simplest options is to use momentjs to get desired date time format.

moment(yourDate).utc().format("DD/MM/YYYY hh:mm:ss a")

Below is code:

let date = moment("2018-06-13T13:28:14+0000").utc().format("DD/MM/YYYY hh:mm:ss a")
console.log(date)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

Convert Numeric String to Readable Date Format in JavaScript

Simple: convert the string into a Date object and use the toLocaleString function.

If you want to get rid of the timezone so the date stays the same wherever the user is you can first convert it into an ISO string, get rid of the 'Z' in the end, and then convert it back into the Date object.