Transform string date to Date object in JS - javascript

My input is get via an API, and I have this array with the following date format as strings:"2022-06-29T15:30:00+00:00", "2022-07-01T09:00:00+00:00", "2022-07-05T16:00:00+00:00".
I want to transform those date in another format (ex for the first one: 29.06.2022 (DD.MM.YYYY)).
Also, I want to compare dates to sort the array. How can I do this? Do I need to convert it into a Date object? (vanilla JS, no frameworks).

You can consider doing it like this:
const str = "2022-06-29T15:30:00+00:00";
const date = new Date(str);
console.log(date); // 👉️ Wed Jun 29 2022 22:30:00
As for sorting array based on dates, you can take a look at How to sort an object array by date property.

You can first sort the dates using sort method
dateArr.sort((a, b) => new Date(a) - new Date(b))
and then map over the array
const dateArr = [
'2022-06-29T15:30:00+00:00',
'2022-07-01T09:00:00+00:00',
'2022-07-05T16:00:00+00:00',
];
const result = dateArr
.sort((a, b) => new Date(a) - new Date(b))
.map((str) => str.split('T')[0].split('-').reverse().join('-'));
console.log(result)

let dateStr = "2022-06-29T15:30:00+00:00";
let getDate = dateStr.split('T')[0];
const date = new Date(getDate);

Related

How to convert each of string dates, e.g., form ''2022-09-27'' to new Date('2022-09-27')

how to convert the array of dates ['2022-09-27', '2022-09-26', '2022-09-29', '2022-09-28', '2022-10-01', '2022-10-02', '2022-10-03'] to new Date ('2022-09-27'),new Date('2022-09-26')
You can create new array using map function.
const dates = ['2022-09-27', '2022-09-26', '2022-09-29', '2022-09-28', '2022-10-01', '2022-10-02', '2022-10-03'];
const new_dates = dates.map((date) => {
return new Date(date);
});
console.log(new_dates);

How to sort by next available date?

I have an array of objects with a date format that looks this way:
"offline_available": "5/1/2021"
As I understand it is the date formatted by the toLocaleDateString() method.
How can I sort all objects by next available date?
You need to translate your date string into a date object in the comparison function.
yourArray.sort( (a,b) => new Date(a.offline_available) - new Date(b.offline_available))
Start with this - assuming the date is mm/dd/yyyy
const arr = [
{"offline_available": "5/1/2021"},
{"offline_available": "12/13/2020"},
{"offline_available": "5/3/2020"},
{"offline_available": "5/3/2021"}
]
const sorted = arr.slice(0).sort((a,b) => new Date(a.offline_available) - new Date(b.offline_available))
console.log(sorted)
What is "sort by next available date"?

Compare Dates in Array of MongoDB Object with different formats

I am using a datepicker in my frontend which sends a date using an AJAX Request.
Now this date is in the format - 'YYYY-MM-DD'. I have an array of dates which I have to compare this date with and pick out which are equal. This array is from a Mongo DB collection where I have TimeZone included in the field as well, for example - "2020-06-03T00:00:00.000Z"
My Slot Json Object
{
date: [
2020-06-03T00:00:00.000Z,
2020-06-05T00:00:00.000Z,
2020-06-07T00:00:00.000Z
],
__v: 0
}
I have to loop over each date in the date array and compare it with the date I get from frontend.
Let's say user input date is
let userInput = '2020-06-03';
And now I have to compare it with the date array
How do I ensure that the following comparision leads me to get a true value for
'2020-06-03' and '2020-06-03T00:00:00.000Z'
I am looking at a solution which is appropriate when looping over all these array elements.
assuming, that your date array always is at 00:00:00 o'clock, i'd suggest something like this:
const dates = [
'2020-06-03T00:00:00.000Z',
'2020-06-05T00:00:00.000Z',
'2020-06-07T00:00:00.000Z'
];
let userInput = '2020-06-03';
function findDates(date) {
const searchDate = Date.parse(date);
const foundDates = [];
dates.forEach(date => {
const tempDate = Date.parse(date);
if (tempDate === searchDate) {
foundDates.push(date);
}
});
return foundDates;
}
console.log(findDates(userInput));
if there's more to consider, please say so
Have the format of date strings(input) and the array of dates be properly specified for parsing. Then you could straight away use the isSame() to check if the dates are equal
let input = "2020-06-03";
let arr = [
"2020-06-03T00:00:00.000Z",
"2020-06-05T00:00:00.000Z",
"2020-06-07T00:00:00.000Z"
];
let result = arr.map(item =>
moment(item, "YYYY-MM-DD hh:mm:ss zz").isSame(input, "YYYY-MM-DD")
);
console.log(result);
<script src="https://momentjs.com/downloads/moment.js"></script>

How can i change date format in vue js template

This is the format i got
2019-01-08T11:11:00.000Z
This is the format i want
2019-01-08
How can i format date like that?
Note: I don't want moment package
Use the Date constructor to create the new date object from your date string. Then you can create the date in any custom format using this object. See the code below.
const input = "2019-01-08T11:11:00.000Z";
const dateObj = new Date(input);
const year = dateObj.getFullYear();
const month = (dateObj.getMonth()+1).toString().padStart(2, '0');
const date = dateObj.getDate().toString().padStart(2, '0');
const result = `${year}-${month}-${date}`;
console.log(result);

lodash - order dates with skipping year value

var dates = ['2017-02-01', '2017-01-01', '2016-02-03', '2018-02-02', '2014-12-25'];
var orderedDates = dates.sort(); // ['2017-01-01', '2017-02-01', '2018-02-02', '2016-02-03', '2014-12-25']
I have collection of dates(moment objects) from different year. I need to sort this dates only by 'ddMM' format (like skip years).
Is there any way to do this?
create copy of dates array, set same year for all dates - not seem like good solution.
for sorting use lodash .orderBy
You should use the 'MMDD' format and not 'DDMM'.
And it is not necessary to convert it back to a moment, since it is just to sort the array.
var orderedDates = _.orderBy(dates, e => moment(e).format('MMDD'));
You can use orderBy with an array of functions that specify your desired order (where the functions get the date and month from the moment object):
const date = (s) => moment(s, 'YYYY-MM-DD');
const dates = [date('2017-02-01'), date('2017-01-01'), date('2016-02-03'), date('2018-02-02'), date('2014-12-25')];
const result = _.orderBy(dates, [m => m.date(), m => m.month()]);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Categories