I have the database collection as such:
{
data: [
item: {
date: "2022-03-22T08:10:37.023Z"
},
item: {
date: "2022-03-22T08:11:04.023Z"
},
...
]
}
I also have variables start_date and end_date which are momentjs dates. I can compare dates with isAfter() and isBefore() methods.
How to get all items with date between start_date and end_date?
Note: date: "..." is a string and not ISODate object.
data is an array.
I can also convert the date string to momentjs date for comparison.
looks like your date is in ISO format so the moment constructor should accept it, e.g.
moment(date);
To prove it, convert it into a plain js Date object and print it
moment("2022-03-22T08:11:04.023Z").toDate()
// Tue Mar 22 2022 10:11:04 GMT+0200 (Israel Standard Time)
You may want to look at isBetween
moment(date).isBetween(start_date, end_date);
Will return true if date is between those two moments.
After that you can use something like .filter to remove the data items that are not between your range and that's about it.
Related
I want to convert 2021-11-05T08:41:37-05:00 string to something as 11-05-2021 8:41am EST with time zone abbreviation at the end as EST CST PST etc
Is there is a way to capture timezone abbreviation from ISO String?
I have tried following option
var d =new Date(Date.parse('2021-11-05T08:41:37-05:00'));
console.log(d.toLocaleString('en-US', { timeZone: 'US/Eastern' }))
which gives me string as 11/5/2021, 9:41:37 AM but does not contain any zone abbreviation at the end.
How to write a query to find documents which are created after "Aug 1 ,2020,3:58:56 PM".
I need to find documents from Cloudant which is greater than particular date. At Cloudant date is stored as "May 18, 2020, 3:58:56 PM".
when I try to query like below:
{
"selector": {
"$and": [
{
"project.projectId": {
"$eq": 34567
}
},
{
"createdDate": {
"$gte": "Aug 1 ,2020,3:58:56 PM"
}
}
]
}
}
I am getting 2019 data also. The proper search is not working.
What is the right way to filter date?
The date in your documents is a non-comparable string. You're asking Cloudant Query to do a lexicographic string comparison. For that to work, your date strings need to be in a comparable format, like "YYYY-MM-DD HH:MM:SS". However, the better solution is to use a numeric datetime.
If you don't want to modify your documents to use a different format, create a map-reduce view that first parses the date string, and then emits a vector-valued key:
d = Date.parse(doc.createdDate);
emit([doc.projectId, d.getFullYear(), d.getMonth()+1, d.getDate()], [...]);
and you can do elegant range queries from this view.
Some useful examples and other date-related tricks here: https://blog.cloudant.com/2018/08/24/Time-sortable-document-ids.html
I'm successfully fetching some data using Axios and I need to store this data to my Postgres database.
One of the fields from the fetched data is a pubDate field, which is formatted exactly like this:
"Tue, 15 Apr 2020 20:01:30 +0000"
Reading through Moment docs, this seems to be this "locale aware 'llll'" format.
So, what I need is to parse this pubDate and format it as the default Postgres date format. The pubDate would then look something like this:
"2020-04-14T20:01:30"
I've been searching for this process for quite some time and just wasn't able to find anything that would work.
I've tried this (did not work), based on the String + Format from docs:
let date = moment(pubDate, 'llll').format();
I'm new to NodeJS, sorry if I messed up something. Thank you in advance!
Looks like MomentJS doesn't have a default function for this (I couldn't find anywhere), so I've created one:
function parseDate (date) {
// date format: "Thu, 16 Apr 2020 18:29:49 +0000"
date = String(date);
// date format (after , split): ["", "Day", "Abbreviated Month", "Year", "hour:minute:seconds", "+0000"]. I.E.: ["", "16", "Apr", "2020", "18:29:49", "+0000"]
let dateInfo = String(date).split(",")[1].split(" ").slice(1, -1);
// ["Day", "Abbreviated Month", "Year", "hour:minute:seconds"]
let dateString = dateInfo[2] + '-' + moment().month(dateInfo[1]).format("M") + '-' + dateInfo[0] + ' ' + dateInfo[3];
const parsedDate = moment(dateString, 'YYYY-MM-DD hh:mm:ss');
return parsedDate;
}
I want to format the date object to a string date format - However, I get a cast error:
Cast to date failed for value "11/2020" at path "startdate"
The start date property is from type Date:
testSchema = new Mongoose.Schema({
startdate: {
type: Date
});
It's saved as type date in the collection - but later I need to get this object entry and change the startdate to a formatted string for further processing.
I tried it as following:
myObject.startdate = moment(myObject.startdate).format('MM/YYYY');
Then I get the cast error.
Is it even possible to achieve what I want? If no, how should I handle this then?
I just created a new array entry and wrote the formatted string into it. Couldn't find solution for the problem
I'm reading a file's modification date with fs.statSync() and I'd like to compare it with a String that comes from my database:
This is my file's last modification date:
Fri Mar 24 2017 13:22:01 GMT+0100 (Central Europe Standard Time)
And I'd like to compare with this string:
2016-07-18 12:28:12
How can I do this with Node.JS? Can I somehow create a new date from the String?
You can use Moment parse both of those formats and compare the results.
var date1 = moment(string1, format1, true);
var date2 = moment(string2, format2, true);
if (date1.diff(date2) === 0) {
// equal
} else {
// not equal
}
See this answer for parsing dates in different formats:
Check if a string is a date value
Make sure that you parse it with explicit format and you don't let it guess the format, or otherwise you will never be sure if it guessed correctly.