Date d = new Date()
iso = d.toISOString()
it gives = 2019-10-28T11:23:52.384Z
but I want in 2019-10-28T01:12 (YYYY-MM-DD T HH:MM AM/PM) only date time with AM/PM
because in mobile view date time getting iso format.
Date d = new Date()
iso = d.toISOString()
it gives = 2019-10-28T11:23:52.384Z
but I want in 2019-10-28T01:12 (YYYY-MM-DD T HH:MM AM/PM) only date time with AM/PM
because in mobile view date time getting iso format.
Use moment.js
moment().format("YYYY-MM-DDThh:mm, A");
Or use Constructor
let d = new Date()
let iso = moment(d).format("YYYY-MM-DDThh:mm, A");
got: "2019-10-28T08:18, PM"
notes:
2019-10-28T11:23:52.384Z
You can use a javascript library known as momentjs
Using their format function you can manipulate the date format to your liking.
for your case, it should be
import moment from "moment"
let date = new Date();
let yyyymmdd = moment(date).format("YYYY-MM-DD");
let hhmm = moment(date).format("LT");
let ans = yyyymmdd + " T " + hhmm;
output:
2019-10-28 T 6:28 PM
Is this useful?
const pad = num => ("0" + num).slice(-2)
const d = new Date(2019, 9, 28, 3, 1, 1)
const iso = d.toISOString()
let [date, time] = iso.split("T")
const [_, hh, mm, ss, ampm] = new Date(iso).toLocaleTimeString('en-US').match(/(\d{1,2}):(\d{2}):(\d{2}) (\w{2})/)
console.log(date + "T" + pad(hh) + ":" + pad(mm) + " " + ampm)
You can strip out the date components and then piece them back in the order you would like.
Hours and meridian will need to be adjusted.
const DateComponenents = (() => {
const fields = ['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss', 'SSS'];
const pattern = /^(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z$/;
return (date) => {
return date.toISOString().match(pattern).slice(1).reduce((r, v, i) => {
return Object.assign(r, { [fields[i]]: v });
}, {});
}
})();
function formatDate(date) {
let c = DateComponenents(date); // Extract the componenents
let hours = parseInt(c['HH'], 10); // Grab the 24-hour piece
let meridiem = hours < 12 ? 'AM' : 'PM'; // Determine AM or PM
hours %= 12; // Modular 0 - 12
if (hours === 0) hours = 12; // Shift 0am to 12am
hours = ('00' + hours).substr(-2); // Pad the hours
return `${c['YYYY']}-${c['MM']}-${c['DD']} T ${hours}:${c['mm']} ${meridiem}`
}
console.log(formatDate(new Date()));
.as-console-wrapper { top: 0; max-height: 100% !important; }
Here is a more dynamic version that supports custom formatting...
const DateComponenents = (() => {
const fields = ['YYYY', 'MM', 'DD', 'HH', 'mm', 'ss', 'SSS'];
const pattern = /^(\d{4})\-(\d{2})\-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z$/;
const applyRest = (c) => {
let hours = parseInt(c['HH'], 10);
let meridiem = hours < 12 ? 'AM' : 'PM';
hours %= 12; hours = ('00' + (hours === 0 ? 12 : hours)).substr(-2);
return Object.assign(c, { 'hh' : hours, 'A' : meridiem });
}
return (date) => {
return applyRest(date.toISOString().match(pattern).slice(1).reduce((r, v, i) => {
return Object.assign(r, { [fields[i]]: v });
}, {}));
};
})();
function formatDate(date, format) {
return (c => format.replace(/\w+/g, key => c[key] || key))(DateComponenents(date));
}
console.log(formatDate(new Date(), 'YYYY-MM-DD T hh:mm A'));
.as-console-wrapper { top: 0; max-height: 100% !important; }
I suggest you use the js standard library Intl.DateTimeFormat API's. They give you a lot of control and are built-in.
They are new-ish but are supported by all browsers other then MSIE.
const options = {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: '2-digit',
minute: 'numeric',
hour12: true // Gives you the AM PM
};
// Note that the 'second' is omitted
// See 'timeZone' and 'timeZoneName' options if you want to control those.
const dateString = new Intl.DateTimeFormat( 'en-ca', options ).format( new Date() );
Note that I specified Canadian English for the locale because that is one of the many countries that has adopted ISO 8601 as their recommended format. The only drawback of 'en-ca' is that it adds a comma but you can just use string.replace to swap that for the 'T'.