I am fetching time from the database to my app.
I want to fetch time from this 2018-10-17 14:52:52 format to this 2:52 PM, 17 Oct format.
How can I format the time without using the 'moment' library?
const input = new Date('2018-10-17 14:52:52');
const output = input.toLocaleTimeString('en-US', {timeStyle: 'short'}) + ', ' + input.toLocaleDateString('en-US', {day: 'numeric'}) + ' ' + input.toLocaleDateString('en-US', {month: 'short'});
console.log(output); // 2:52 PM, 17 Oct
Related
Is there any way to convert normal string 5pm into 2022-04-20T17:00:00.000Z format?
I have got this from backend but Im using timepicker in antd. It only accepts 2022-04-20T17:00:00.000Z format and it is in string format in my DB.
This should consistently give you the current date with the time attached
function convertToISO(timeString) {
const [hour12, ampm] = timeString.split(/(?=[ap]m$)/i)
const hour = hour12 % 12 + (ampm.toLowerCase() === 'pm' ? 12 : 0)
const date = new Date()
// Set time, adjusted for time zone
date.setHours(hour, -date.getTimezoneOffset(), 0, 0)
return date.toISOString()
}
console.log(convertToISO('5pm'))
When I tried to convert 2020-12-14 to 14 Dec 2020 by using
1st Method
<small>{item.date}</small>
2nd Method
{new Date(item.date).toLocaleString()}
then I got below output
2020-12-14
12/14/2020, 5:30:00 AM
Is there any way to convert the date format from 2020-12-14 to 14 Dec 2020.? in reactjs
A small modification to this elegant answer by Dave splits the toString date string into an array and formats it into the result you want. Check the code below:
const date = new Date(2020, 11, 14).toString().split(" ");
// ["Mon", "Dec", "14", "2020", "14:05:53", "GMT+0100", "(Central", "European", "Standard", "Time)"]
console.log(date[2] + " " + date[1] + " " + date[3]);
// 14 Dec 2020
using moment package
moment(moment('2020-11-18', 'YYYY-MM-DD')).format('DD MMM YYYY');
I tried different method to develop date picker in reactJS. I done with "react-datepicker" package and it return "Wed May 15 2019 12:54:33 GMT+0100" as a result but I need 12/12/2000 format
try to format it as you want
const pickerDate = new Date('Wed May 15 2019 12:54:33 GMT+0100')
const day = pickerDate.getDay() < 10 ? '0' + pickerDate.getDay() : pickerDate.getDay()
const month = pickerDate.getMonth() + 1 < 10 ? '0' + (pickerDate.getMonth() + 1) : pickerDate.getMonth() + 1
const year = pickerDate.getFullYear()
const formatedDate = `${day}/${month}/${year}`
you can also use third part libraries like moment
Actually in my app. I am trying to display current date & time in a specific format like this " 27/02/2019 1:40 PM ". I have done this by making use of custom formatting codes. But what i actually need is, I need to achieve this by making use of libraries.
Thanks for helping.!
Using this:(Manually formatting Date & Time)
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hours = d.getHours();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12;
var min = d.getMinutes();
min = min < 10 ? '0'+min : min;
var result = date + '/' + month + '/' + year + ' ' + hours + ':' + min + ' ' + ampm;
console.log(result); //Prints DateTime in the above specified format
Use moment.js https://momentjs.com/
moment().format('DD/MM/YY h:mm A') will give you your desired output.
Please find below code:
var sReportDate = new Date();
var sReportFormattedDate = sReportDate.toDateString() + " " + sReportDate.toLocaleTimeString() + " " + sReportDate.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
sTitle = sTitle + "\nReport Extracted on:" + sReportFormattedDate +"\n\n";
return sTitle;
The code formats the date. And the returned value is used in exported csv of angular-ui-grid.
When I export the grid from chrome and opens csv file in MS Excel, the date displays properly. But when I export the same grid from IE 11 and opens the csv file in MS Excel, the date displays with so many special characters.
The below statement displays the date in correct format on IE 11 console:
var sReportFormattedDate = sReportDate.toDateString() + " " + sReportDate.toLocaleTimeString() + " " + sReportDate.toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1];
Tue Sep 22 2015 3:17:35 PM GMT+0800 (Malay Peninsula Standard Time)
But while export from IE 11, the time portion (string between 2015 and PM) displays many special characters in MS Excel.
Why toLocaleTimeString is exported properly from Chrome and why is it not exported properly from IE11 ?
How do I display the date in proper format while export from IE 11 ?