How to create Google calendar link date in Javascript 2022? - javascript

In 2022 the create Google calendar event link looks like this:
https://calendar.google.com/calendar/u/0/r/eventedit?sf=true&output=xml&text=sometext&location=somelocation&details=somedetails&dates=20220418T013000Z/20220416T020000Z
How do you formate such date in Javascript?
const formatDate = (date) => {
???
};
const myDate = new Date();
const myFormattedDate = formatDate(myDate);
console.log(myFormattedDate)
expecting output:
20220418T013000Z
Any nice looking and easy solution (rather than getHours(),getMinutes(),etc.)?

JS Dates have an inbuilt .toISOString() method which gets the right format, then just remove special characters:
let date = new Date();
let isoDate = date.toISOString()
let formattedDate = isoDate.replace(/[^\w\s]/gi, '');
console.log(formattedDate)

Related

Use date-fns to format day as UTC

I have the following date format 2022-07-20 and I would like to parse into the following format 2022-07-20T00:00:00.000Z
I'm using the following code
const main = () => {
const day = '2022-07-20'
const date = new Date()
const result = parse(day.toString(), 'yyyy-MM-dd', date)
console.log(result.toISOString())
}
And getting the following output 2022-07-19T21:00:00.000Z. I assume this happening because my timezone is UTC+3.
How do I get it formatted as 2022-07-20T00:00:00.000Z?

Add days to Date object and then converting to local string?

How can i add 5 days to the current date, and then convert it to a string representing the local date and time?
const newDate = new Date();
const test = newDate.setDate(newDate.getDate() + 5).toLocaleString();
Just returns the number of milliseconds.. (same if i use toString().
Without using any libraries, Vanilla JS solution:
const now = new Date()
const inFiveDays = new Date(new Date(now).setDate(now.getDate() + 5))
console.log('now', now.toLocaleString())
console.log('inFiveDays', inFiveDays.toLocaleString())
This even works when your date overflows the current month.
Just use new Date() in front of it.
const newDate = new Date();
const add =5
const test = newDate.setDate(newDate.getDate() + add)
console.log(new Date(test).toLocaleString());
The easiest way is by using a date library like Moment.js or date-fns. I gave an example below using date-fns and addDays
const today = new Date();
const fiveDaysLater = addDays(newDate, 5);

Getting 'NaN' while trying to convert a string of date to milliseconds [duplicate]

This question already has answers here:
Why does Date.parse give incorrect results?
(11 answers)
Closed 2 years ago.
Tried this:
1.
const today = new Date('28.08.2020');
const milliseconds = today.getTime();
const today = Date.parse("28.08.2020")
var today = new Date('28.08.2020');
var milliseconds = today.getMilliseconds();
Getting NaN while trying to convert a string of date to milliseconds
Better to change date format to YYYY-MM-DD as suggested in other answer
Or you can do something like this
var from = '28.08.2020'.split(".");
var today = new Date(from[2], from[1] - 1, from[0]);
const milliseconds = today.getTime();
console.log(milliseconds);
You use the incorrect format. If you get the date from backend you should convert it.
const date = '28.08.2020';
const [day, month, year] = date.split('.');
const validDate = new Date();
validDate.setFullYear(year);
validDate.setDate(day);
validDate.setMonth(month);
// or just
const validDate2 = new Date(year, month, day);
const milliseconds = validDate.getTime();
const milliseconds2 = validDate2.getTime();
console.log(milliseconds)
console.log(milliseconds2)
After this conversion you can use the date as you want
Assuming that you do not want to manually parse the string, you could try to use moment library, which allows one to provide custom dateString patterns used for parsing the date, like demonstrated below
const dateString = '28.08.2020';
const date = moment(dateString, "DD.MM.YYYY");
console.log("date", date); // displayed zulu time might be different than your local timezone
console.log("milliseconds", date.valueOf());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
Please take a note that moment will accept the date in your local timezone, which may pose some issues. If you want to make up for it, you should look up moment-timezone library
Oh, in that case you can change the imput to the "yyyy-mm-dd", is that a posibility?
const date = '28.08.2020';
let dateFromat = date.split('.');
dateFromat = `${dateFromat[2]}-${dateFromat[1]}-${dateFromat[0]}`;
const today = new Date(dateFromat);
const milliseconds = today.getTime();
output: 1598572800000
the dating format is wrong.
new Date('2020-08-28') should work

How do I convert date mm/dd/yyyy to yyyy-mm-ddT00:00:00.000Z format in javascript

var dateType = $filter('date')($scope.datalist[0].OVERRIDE_DATE, "MM/dd/yyyy");
I have the output : 05/07/1992
I want to change above date to yyyy-mm-ddT00:00:00.000Z format ?
toISOString() should do what you want.
e.g.
new Date('05/07/1992').toISOString();
will give you
"1992-05-07T07:00:00.000Z"
The Date.prototype.toISOString() method may be what you want:
const rawDate = $filter('date')($scope.datalist[0].OVERRIDE_DATE, "MM/dd/yyyy");
const formattedDate = new Date(rawDate).toISOString();
or, if $scope.datalist[0].OVERRIDE_DATE is already a date object, you could just do:
const formattedDate = $scope.datalist[0].OVERRIDE_DATE.toISOString();
Link to Date.prototype.toISOString docs

Get custom date time format in moment.js

I have time saved in database like this
2016-01-30 14:36:00
But I am using moment.js and I want to convert the time as in this format
2016-01-30T14:36:00
So to get that format I tried like this
var date = new Date();
console.log(moment(date).parseZone().format());
but its showing date like this
2015-12-21T15:20:04+05:30
So can someone tell me how to get the time in that format in moment.js
try this
var dbDate = '2016-01-30 14:36:00';
var format = 'YYYY-MM-DDTHH:mm:ss';
var date = moment(dbDate, format);
var isValid = date.isValid() // true
console.log(isValid, date.year(), date.month(), date.date())
Try using the below code -
console.log(moment(new Date()).format('YYYY-MM-DD[T]HH:mm:ss'));
Use a custom format:
var date = moment();
var out = date.format('YYYY-MM-DDTHH:mm:ss');
console.log(out);
This will help you
var currentDate = moment(new Date()).format('YYYY-MM-DDTHH:mm:ss');
//2015-12-21T15:50:28

Categories

Resources