How do I go about comparing new Date().toUTCString() to a say Wed, 03 Oct 2018 01:33:29 GMT in moment?
I've tried moment(Wed, 03 Oct 2018 01:33:29 GMT).isBefore(moment(new Date().toUTCString())) but it doesn't seem to work.
The way you have explained, the comparison is working for me. Check the snippet. FYI, you need to use '' while creating a moment object from the string.
$(() => {
var a = moment('Wed, 03 Oct 2018 01:33:29 GMT');
var b = moment(new Date().toUTCString());
console.log(a.isBefore(b));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can simply do it as follows:
moment('Thu Oct 04 2018 07:35:54 GMT+0530 (IST)').isBefore(moment())
moment('Wed, 03 Oct 2018 01:33:29 GMT').isBefore(moment())
This should return you value. (true/false)
Related
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)
);
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();
I want to generate this date format using Moment.js:
Mon Apr 1 17:51:40 2019
Right now, I am getting this format instead:
Mon Apr 01 2019 17:51:40 GMT+0530 (IST)
use this:
Moment(new Date(this.state.date)).format('MMMM Do YYYY, h:mm:ss a')
or whatever format you want. you can check them on here: https://momentjs.com/
Try this:
var dateTime = new Date("Mon Apr 01 2019 17:51:40 GMT+0530 (IST)");
dateTime = moment(dateTime).format("ddd MMM D HH:mm:ss YYYY");
It's already a valid date string - just make a new Date:
console.log(new Date("Mon Apr 01 2019 17:51:40 GMT+0530 (IST)"));
first you need to install moment
let formatedDate = moment('Mon Apr 01 2019 17:51:40 GMT+0530 (IST)').format('ddd MMM DD HH:mm:ss YYYY');
console.log(formatedDate)
output: Mon Apr 01 17:51:40 2019
Done You can check it
I'm using Moment.js to make a Resource Calendar and I need an array of dates for this week. The console log of my current function prints out appropriately but the array that gets pushed in for each date is wrong.
var startOfWeek = moment().startOf('week');
var endOfWeek = moment().endOf('week');
var days = [];
var day = startOfWeek;
do {
console.log(day._d);
days.push(day._d);
day = day.add(1, 'd');
}
while (day <= endOfWeek);
console.log(days);
Returns:
Sun Jan 18 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Mon Jan 19 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Tue Jan 20 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Wed Jan 21 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Thu Jan 22 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Fri Jan 23 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
Sat Jan 24 2015 00:00:00 GMT-0500 (EST) schedule.js?320233fd69f9859ccb55248b608e15891032b17d:31
[Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST), Sun Jan 25 2015 00:00:00 GMT-0500 (EST)]
Notice how the array at the bottom is the next date in the array repeated 7 times.
As danludwig mentioned in his comment on the question, you are adding a reference for the same date to the array multiple times.
From the Moment.js documentation:
It should be noted that moments are mutable. Calling any of the manipulation methods will change the original moment.
If you want to create a copy and manipulate it, you should use moment#clone before manipulating the moment.
You should be calling the clone function on the Moment date object as shown here.
var startOfWeek = moment().startOf('week');
var endOfWeek = moment().endOf('week');
var days = [];
var day = startOfWeek;
while (day <= endOfWeek) {
days.push(day.toDate());
day = day.clone().add(1, 'd');
}
console.log(days);
As an aside:
You should not reference internal fields/functions of a 3rd party library. The name of these references are more likely to change than the public API described in the documentation. _d can be referenced by calling the public function toDate.
I have this string "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" and I need to convert it to the timezone (GMT-0300) using just moment.js (not moment-timezone.js)
I'm doing this but it's returning the same hour.
var startDateTime = moment(date).format('YYYY-MM-DD HH:mm Z'),
startMoment = moment.parseZone(startDateTime).zone();
console.log(moment(startDateTime).zone(startMoment).format("YYYY-MM-DD HH:mm"));
Any help ?
To convert a moment object with timezone A to timezone B, you can do the following:
var startDateTime = "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)";
var newDateTime = moment(startDateTime).zone('-0400').format('YYYY-MM-DD HH:mm')
Also note that in your example, the initial time "Mon Oct 21 2013 21:00:00 GMT-0300 (ART)" is already GMT-0300.