Js new Date() creates different GMT - javascript

I'm reading data from a API server:
opTMP:
{ DATAI: "2019-10-27T00:00:00", …}
{ DATAI: "2019-10-31T00:00:00", …}
then I create a new date:
const opTMP1 = this.opTMP.map(x => Object.assign({}, x));
for (const op of opTMP1){
let d = new Date(op.DATAI);
console.log(d);
...
}
but in console I got different results,one is GMT+0300 and one GMT+0200 :
d: Sun Oct 27 2019 00:00:00 GMT+0300 (Eastern European Summer Time)
d: Thu Oct 31 2019 00:00:00 GMT+0200 (Eastern European Standard Time)
because of that I got problems when comparing it,I want to get only day month and year,no time info needed,how can I reset both to the same time or to 0:00:00?

Converting date to epoch time is good way to comparing the dates.
let d = new Date(op.DATAI).getTime();

Related

Sorting dates including undefined and null

Im trying to sort data in order of ending soonest. this is my code and you can see the console results below. The sort is working somewhat but not completely. Can anyone help?
I see its to do with the invalid date but I can't work out how to get around this. The Invalid date field is blank in the JSON data that's pulled in. Null/undefined I would like to be last.
if (this.sortBy === "ending") {
filteredResults.sort((a, b) => {
var aDate = new Date(a.metaData.t);
var bDate = new Date(b.metaData.t);
console.log(b.metaData.t);
if (!aDate) {
aDate = 99999999; }
console.log("adate" + aDate);
if (!bDate) {
bDate = 99999999;
}
console.log("bdate" + bDate);
if (aDate > bDate) {
return 1;
}
if (aDate < bDate) {
return -1;
}
return 0;
});
}
return filteredResults;
},
Console.log data results
adateWed Nov 30 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
bdateTue Dec 13 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
adateWed Nov 30 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
bdateFri Sep 09 2022 00:00:00 GMT+0100 (British Summer Time)
adateWed Nov 30 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
bdateWed Nov 30 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
adateWed Nov 30 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
bdateThu Dec 08 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
adateWed Nov 30 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
bdateMon Dec 05 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
adateInvalid Date
bdateTue Dec 13 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
adateInvalid Date
bdateFri Mar 31 2023 00:00:00 GMT+0100 (British Summer Time)
adateInvalid Date
bdateSun Aug 20 2023 00:00:00 GMT+0100 (British Summer Time)
adateInvalid Date
bdateTue Jan 01 2030 00:00:00 GMT+0000 (Greenwich Mean Time)
adateInvalid Date
bdateTue Dec 13 2022 00:00:00 GMT+0000 (Greenwich Mean Time)
Invalid Date
Filtered results - the JSON is not exact here as its too big to add to this question. The format of the data is correct locally
(1 example I have XX most of the data for privacy reasons)
filteredResults {
K:"XX"
D:"XX"
N:"XX"
y:"XX"
t:"3 August 2022"
u:"XX"
X:"XX"
R:"XX"
T:"XX"
E:"XX"
}
.....
80 more rows like this but with various dates
You could check the value in advance and move unknown dates to the end of the array.
// assuming iso 8601 dates (same time zone or zulu), null or undefined
data.sort((a, b) =>
!a.date - !b.date ||
a.date.localeCompare(b.date)
);

Unable to increment hour in JavaScript Date() object and get expected Object output

I'm trying to get two datetime object, where the second one is one hour greater than the first one:
Sat Jan 30 2021 01:56:53 GMT+0600 (Bangladesh Standard Time)
Sat Jan 30 2021 02:56:53 GMT+0600 (Bangladesh Standard Time)
My code looks like this:
minStartDate = new Date();
minEndDate = new Date();
ngOnInit() {
console.log(this.minStartDate);
console.log(this.minEndDate);
console.log(this.minEndDate.setHours(this.minEndDate.getHours() + 1));
}
The output I'm getting is:
Sat Jan 30 2021 01:56:53 GMT+0600 (Bangladesh Standard Time)
Sat Jan 30 2021 01:56:53 GMT+0600 (Bangladesh Standard Time)
1611964613955
So, as #Mike suggested on comments, Using moment.js solved the problem for me.
The final code is:
import * as moment from 'moment';
minStartDate = new Date();
minEndDate = new Date();
ngOnInit() {
console.log(this.minStartDate);
console.log(this.minEndDate);
console.log(moment(this.minEndDate).add(1, 'hours').toDate()); //expected output
}
The output:
Sat Jan 30 2021 02:30:08 GMT+0600 (Bangladesh Standard Time)
Sat Jan 30 2021 02:30:08 GMT+0600 (Bangladesh Standard Time)
Sat Jan 30 2021 03:30:08 GMT+0600 (Bangladesh Standard Time)

Convert all dates in an array to format MM/DD/YYYY javascript

I have an array of dates in the format :
dates_arr = [
Thu Aug 13 2020 00:00:00 GMT-0400 (Eastern Daylight Time),
Fri Aug 14 2020 00:00:00 GMT-0400 (Eastern Daylight Time),
Sat Aug 15 2020 00:00:00 GMT-0400 (Eastern Daylight Time),
Sun Aug 16 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
]
I would like to convert all the dates to the format MM/DD/YYYY and replace the array with them.
I would like to convert all the dates to the format MM/DD/YYYY and
replace the array.
You could do it as follows -
Correct Code -
let dates_arr = [
'Thu Aug 13 2020 00:00:00 GMT-0400 (Eastern Daylight Time)',
'Fri Aug 14 2020 00:00:00 GMT-0400 (Eastern Daylight Time)',
'Sat Aug 15 2020 00:00:00 GMT-0400 (Eastern Daylight Time)',
'Sun Aug 16 2020 00:00:00 GMT-0400 (Eastern Daylight Time)'
];
dates_arr = dates_arr.map((element) => {
var d = new Date(element);
return `${d.getMonth()+1}/${d.getDate()}/${d.getFullYear()}`;
})
console.log("Dates in the format MM/DD/YYYY : \n", dates_arr);
Explanation :
The strings which are present in dates_arr are the default way in which Javascript will output the dates. We are taking each of those strings and mapping over the dates_arr using map() function and simply returning the dates in the required format. This will create a new array with strings of date in the form we need them to be.
Firstly, I am creating a date object in the code using new Date(element). The Date() constructor can take in date string and form date object on which we can apply various in-built methods provided by JS as given below.
There are different methods which date provides us like
getMonth() which returns the month from 0-11
getDate() which returns the day of the month from 1-31 and
getFullYear() which returns the year in the date.
I have used these methods to get the date string into the desired format. There are various other methods as well which you can read more on here.
Also, if you are not aware, I am using template literals in the return statement which is just a syntactic sugar added in ES6. The return statement does the same thing as -
return ((d.getMonth()+1) + '/' + d.getDate() + '/' + d.getFullYear())
But using the template literals is just a more efficient way of writing such statements.
var formattedDate = dates_arr.map(date=>dateFormat(date));
function dateFormat(date){
const dateTimeFormat = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' })
const [{ value: month },,{ value: day },,{ value: year }] = dateTimeFormat .formatToParts(date )
return `${month}/${day}/${year}`
}

Find max date from a string array of dates using max function of date-fns

Dates string array is constructed from backend data object as below:
const dates: string[] = this.data.map((item:any) => item.date.jsdate);
The result is
dates = [
Thu Jan 12 2015 00:00:00 GMT+0530 (India Time),
Tue Feb 25 2015 00:00:00 GMT+0530 (India Time),
Sun Jan 28 2016 00:00:00 GMT+0530 (India Time)
]
typeof(dates) is "object"
How to find the get the latest date among the jsdates array?
Is it possible to apply max function of date-fns?
I can offer one solution, using the momentjs (https://momentjs.com/) library.
You can sort the dates using:
dates = dates.sort((a,b) => moment(a).diff(moment(b));
Then if you want the latest date you could take the last element from that array:
return dates[dates.length-1];
You should also be able to do this without moment:
dates = dates.sort((a,b) => new Date(b) - new Date(a));
Convert each to a Date object and compare using getTime() function.
var dates = [
'Thu Jan 12 2015 00:00:00 GMT+0530 (India Time)',
'Tue Feb 25 2015 00:00:00 GMT+0530 (India Time)',
'Sun Jan 28 2016 00:00:00 GMT+0530 (India Time)'
]
console.log(
dates.sort((a,b)=>new Date(b).getTime() - new Date(a).getTime())[0]
)

Javascript Date -> numeric vs string

var date1 = new Date('1900-01-01');
console.log(date1);
Yields:
"Mon Jan 01 1900 01:00:00 GMT+0100 (W. Europe Standard Time)"
var date2 = new Date(1900,1,1);
console.log(date2);
Yields:
"Thu Feb 01 1900 00:00:00 GMT+0100 (W. Europe Standard Time)"
Fiddle
But I don't understand why!
You can see the month difference since when you pass individual components (year, month, day, etc) to the Date object constructor, you have to consider that month parameter should start with 0:
console.log( new Date('1900-01-01').getMonth() ); // 0
Other than Jan/Feb there shouldn't be any differences in dates.
MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

Categories