Gibran Shah
Gibran Shah

Reputation: 1109

how to get this specific datetime format in javascript

In javascript, is there a way to convert a date time to the following format:

// 11/3/18, 12:00 AM

Date().toString() gives me:

Sat Nov 03 2018 00:00:00 GMT+0000 (UTC)

Thanks.

Upvotes: 0

Views: 124

Answers (2)

Victor
Victor

Reputation: 888

tl;dr: try typing this in your browser's javascript console on the moment.js website: moment().format('MM/d/YY h:mm A')

Three things:

1. If you haven't already, check out these date docs for the API:

2. Without an external library

See Ele's answer above for most elegant non-library: https://stackoverflow.com/a/53135859/3191929

Ex. Extract mm/dd/yy from Date

const root = new Date();
let month = root.getMonth(); // 0 to 11
let day = root.getDate(); // 1 to 31
let year = root.getFullYear(); year = String(year).slice(2);
// 11/3/18, 12:00 AM mm/dd/yy, hh:mm AM/PM
const output = ``${month}/${day}/${year}``; // mm/dd/yy

And from there you can explore the API to get the 24 hours, then do a check for AM/PM and build the result etc etc. (see bbram's answer here: https://stackoverflow.com/a/8888498/3191929 for the relevant Date APIs for time)

Here's a quick'n'dirty solution to your specific question

Ex. Extract mm/dd/yy hh:mm AM/PM from Date

function formatDate(root) {
    let month = root.getMonth(); // 0 to 11, 0 = Jan
    month += 1; // 1 to 12, 1 = Jan
    let day = root.getDate(); // 1 to 31
    let year = root.getFullYear();
    year = String(year).slice(2);

// Time transformation appropriated from bbrame
// https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format/8888498#8888498
function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'PM' : 'AM';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}

// mm/dd/yy, hh:mm AM/PM
const output = `${month}/${day}/${year} ${formatAMPM(root)}`;
return output;
}

var rootDate = new Date();
console.log(formatDate(rootDate)); // mm/dd/yy hh:mm AM/PM

3. With an external library

Using moment.js you can achieve the above with just this:

var root = moment(); // valid moment date
var formatted = root.format('m/d/YY h:mm A');

See the moment.js docs for more details: https://momentjs.com/docs/#/displaying/format/

If momentjs specifically is a no-go, see here for other options as well: https://github.com/you-dont-need/You-Dont-Need-Momentjs

Upvotes: 0

Ele
Ele

Reputation: 33726

This is an alternative to format dates, the function Date.prototype.toLocaleDateString allows you to format date according to options/flags.

Some js engines manage the format process differently (so, this is implementation dependent), therefore be careful. Further, you need to check for compatibility in browsers.

let today = new Date();
var options = { year: 'numeric', month: 'numeric', day: 'numeric', hour12: true, hour: 'numeric', minute: 'numeric' };

console.log(today.toLocaleDateString('en-US', options));

Upvotes: 1

Related Questions