How to format this date so it's more readable? - javascript

I have date in this format:
2022-06-28T17:09:00.922108+01:00
I want to covert it into a more readable format.
I'm using javascript/react and I tried moment-js but it gives me "invalid format".
What are my other options of turning it into a normal date?

You can use this to format date:
const format = "DD-MM-YYYY"
moment(date).format(format)
or
moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2022, 9:49:49 pm
for more detail refer: https://momentjs.com/

If you are using moment.js you can convert this to any format you like using this doc
Sample code
moment('2022-06-28T17:09:00.922108+01:00').format("DD-MM-YYYY"); //28-06-2022
Kindly go though the moment.js features and documents. There is a lot more you can do with this like showing time, day of the week etc

Related

Format Date in Google App Script to YYYY-MM-DD

I'm trying to format the date in Google App Script and then store it into the Google Sheet. Everything is working fine but the problem is source date has no specific format, dates may come in different formats like DD-MM-YYYY, MM-DD-YYYY, YYYY-MM-DD or date can also come with time and then I've to convert it into YYYY-MM-DD format and then save in google sheet.
I've tried to convert to covert using the below codes:
var date = new Date("01-15-2022").toISOString().split('T')[0] // MM/dd/yyyy
Logger.log(date);
this code works only if the source date has in a format like MM/dd/yyyy and yyyy/MM/dd only if the date is in a format like dd/MM/yyyy then codes does not work.
I want a method that converts all the date formats in a single format like yyyy/MM/dd
I got a little bad news for you. There is no way to distinguish algorithmically between 01-02-2022 (Feb 1, 2022) and 01-02-2022 (Jan 2, 2022). So technically this source data is the infamous 'garbage in'.
Somehow I managed to find an alternative option to achieve this using Moment Js Library below is the solution:
function myFunction() {
let formats = ["DD/MM/YYYY", "MM/DD/YYYY", "YYYY/MM/DD", "DD/MM/YYYY hh:mm:ss", "DD/MM/YYYY hh:mm:ss A", "MM/DD/YYYY hh:mm:ss A", "YYYY/MM/DD hh:mm:ss A"]
Logger.log(convertDate("5/1/2022 12:00:00 PM", formats));
}
function convertDate(date, formats){
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js').getContentText());
let dateMomentObject = moment(date, formats);
let dateObject = dateMomentObject.toDate();
let dateStr = moment(dateObject).format('YYYY-MM-DD');
return dateStr;
}

Convert YYYY-MM-DD HH:MM:SS to different format in Javascript

I have a string in javascript as 2016-02-27 20:24:39 and I want to convert this as 27th Feb 08:24pm.
What is the easiest way to do in Javascript?
Checkout the JavaScript library called moment.js.
Since the default format for moment is ISO 8601 (YYYY-MM-DD HH:MM:SS), you don't need to tell moment how to parse the input String date (it defaults to ISO 8601), so you can simply write:
var now = "2016-02-27 20:24:39";
var formattedDate = moment(now).format("Do MMM HH:mma");
console.log(formattedDate);
Demo:
https://jsfiddle.net/gekd97dy/
More information about displaying in different formats can be read here:
http://momentjs.com/docs/#/displaying/
There is a non-standard Date method toLocaleFormat('%d-%b-%Y'). But appears to only work in Firefox for now.
Better use the date.format library (only 125 lines)
var date = new Date('2016-02-27 20:24:39');
dateFormat(date, "dS mmm, h:MMTT");

how to modify date format in javascript (node js)

I have a date format like 2015-12-07T02:45:00.000Z but want to convert it to like Nove 12 , 2015 4:30 PM or 3 minuts ago accordingly
You can use a library called moment.js. And use
moment().format('MMMM Do YYYY, h:mm a') to convert to your format or
moment().startOf('hour').fromNow() to print out relative time.
Have you looked at moment.js? It has relative conversions which seem to be what you are looking for.

Handle datetime timestamp javascript(angularjs) and mongo for slider

I'm using angularjs and mongodb.
I have not found a angularjs date slider so I'm taking this one. The idea is to use a timestamp with a step a day. So I take this timestamp convert it to a dd mmmm yyyy format and be displayed in the front-end.
Assuming as I said I use mongodb what can be a good solution for the date number format?
If you are looking for a date time library, try moment.js
It is very simple to use. Include the script file using <script> tag. Then use it like this.
moment().format('MMMM Do YYYY, h:mm:ss a');
This will return November 6th 2013, 1:35:00 pm
If you need to show the time in human format use fromNow() like this moment("20111031", "YYYYMMDD").fromNow(); This will result 2 years ago .

Split the date in javascript/jquery

How to split the below date format into day date and time.
Date Format is like "2013-05-07T11:04:00+05:30"
I want to display above date like "Tue,7 May 2013, 11.04AM".
Please suggest how to do this in java script or jquery
Thanks in Advance.
You can use this very useful little library:
http://momentjs.com/
Example:
moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a");
You might be able to use Date.parse(string), but there is no way to determine what date/time formats a particular JavaScript implementation supports.
Otherwise:
Use a regex to break up (and validate) the string, convert each string component into a Number and then pass to the Date constructor taking separate components.
Use a library that implements the previous option (eg. see other answer).
You should work with the native Date Object - this is the easiest way to handle the string.
You can do the following:
var date = new Date("2013-05-07T11:04:00+05:30");
now you have several Date methods you can use to format your string and get the information you need, e.g. what you want is:
date.toUTCString()
// output:
"Tue, 07 May 2013 05:34:00 GMT"
You also could use a regex or an external library, but probably the best way (imo!) is to simply work with the Date Object.
moment.js could be a good choice for you, for example:
Actual moment:
moment().format('MMMM Do YYYY, h:mm:ss a');
Format:
moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a");

Categories

Resources