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

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;
}

Related

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

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

How to convert string date to specific format?

I am trying to get date from string in specific format like:
const format = 'MM/dd/yyyy HH:mm';
const dt = "2021-03-11T22:00:00.000Z"; // expecting "03/11/2021 22:00"
console.log(moment(dt).format(format)); // but getting "03/Th/yyyy 23:00"
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
I tried js date, moment, luxon and I don't understand how to make this :(
I suspect that 000Z made problem but this is date I get.
The difference between Moment.js and Luxon is the case of the format keys. Please review the key/token tables.
Moment.js / String + Format
Luxon / Table of tokens
Note: Moment.js has been deprecated in favor of the team's newer library Luxon and other ECMA Script standards that are being introduced in ECMA-402. If you are considering using Moment.js now, just switch over to Luxon.
If you are using Luxon, you can call .toUTC() right before you format the date. Make sure your day of month (dd) and year (yyyy) format keys are lower-case.
const DateTime = luxon.DateTime;
const
format = 'MM/dd/yyyy HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(DateTime.fromISO(dt).toUTC().toFormat(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
If you are using Moment.js, you can call .utc() right before you format the date. Make sure your day of month (DD) and year (YYYY) format keys are upper-case.
const
format = 'MM/DD/YYYY HH:mm',
dt = "2021-03-11T22:00:00.000Z";
console.log(moment(dt).utc().format(format)); // 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
Your format string is wrong, it should be: 'MM/DD/YYYY HH:mm'.
Then, if your date comes with ':000Z', you should remove it with the substring() method.
Working code:
const format = 'MM/DD/YYYY HH:mm';
const dateString = "2021-03-11T22:00:00:000Z";
const date = dateString.substring(0, dateString.length - 5);
console.log(moment(date).format(format)); // prints 03/11/2021 22:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
You need to use YYYY instead of yyyy and DD instead of dd to get the expected result.
By the way, I suggest using another library than Moment, like dayjs for instance.

Javascript convert timezone Z to +0000 with moment

The backend API I am working with only understands timezone in +0000 format.
I currently I get a date in the current format:
"2017-12-20T16:39:31.000Z"
With moment.js how would I get it in the following string format?
2017-12-20T16:39:31+0000
So far I have done:
var date = new Date();
this.lastCheckedDate = moment(date).toISOString();
You can use the .format() method to format the date in your own way:
moment("2017-12-20T16:39:31.000Z").format('YYYY-MM-DDTHH:mm:ssZZ');
If the date is in another timezone, I recommend using .utc() first to convert the timezone to UTC, and then format the date:
moment("2017-12-20T16:39:31.000+0530").utc().format('YYYY-MM-DDTHH:mm:ssZZ');

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 convert time and date string to ISO format

I have two inputs, a time and a date input. I'm trying to format them as an ISO string to send to the backend using moment.js.
This is what I have so far 01:00 2016-01-01, I need to format or convert that to ISO. Is there a way to do it using Moment?
To convert ISO I recommend the more standard
date.format();
or
JSON.stringify(yourDate)
or if you prefer momentjs:
var date = moment();
date.toISOString();
or
moment(yourDate).format('MM/DD/YYYY'); // <- your custom format string
To know what are the momentjs formatting rules start reading here
Assuming you are referring to ISO8601 and momentjs (2.10.6), I currently do it like this
var example = momentObject.format("YYYY-MM-DD[T]HH:mm:ss");
You need to use moment's parse function to first create the correct moment object from the data that you have (assuming a 24-hour clock, and month listed before the days):
var myMoment = moment("01:00 2016-01-01", "HH:mm YYYY-MM-DD");
Then you can use moment's format function to output the date in the ISO format that you want. Note that calling the format function without any parameters will output ISO 8601 by default:
myMoment.format();
See the moment docs for more info here.
Hope this helps!

Categories

Resources