Get MongoDB items matching date - javascript

I am trying to get a count on posted item from meteor-mongo matching date periods.
I inserted my posts dates as such.
posts
submitted: new Date()
the dates in the database have the following format.
yyyy-mm-dd 16:16:34.317Z // I do not understand the last part (what format it is)
I have tried this to get match the date of today from the submitted field
var currentDate = new Date();
var dd = currentDate.getDate();
var mm = currentDate.getMonth()+1;
var yyyy = currentDate.getFullYear();
var today = yyyy+'-'+mm+'-'+dd;
Posts.find({submitted: today}).count()
However, the last part is returning 0.
Is it because the last hh,mm,ss part of today is missing? If so, how can I tell meteor-mongoto ignore the time part of date so that I can return that count?

I don't like to deal with JS date objects formats, and i guess you either (I do not understand the last part (what format it is))
Give a try to momentjs package, the documentation its pretty clear.
So on the insert you can have something like.
var today = moment().format(''MMMM Do YYYY, h:mm:ss a'); // April 3rd 2015, 12:17:06 pm
Posts.insert({subbmited:today})
and do a simple find like this.
var endQuery = today..add('days', 3).calendar(); // just an example.
Posts.find({submitted: {$gt: today,$lt:endQuery}})
Here thanks to #Larry Maccherone point we are using $gte (grater than) and $lt (less than), so this works like find me post between the post submitted day and the post submitted dat + 3 days (like you ask managing ranges)

You are storing your submitted date in MongoDB with both time and timezone information. Performing a direct comparison of your currentDate with the submitted date in Mongo will never be true, since your currentDate does not contain time information.
Also you are using a String data type to query the date, which also will not work since you need to use a Date data type. Something like this will work for you:
var today = new Date();
today.setHours(0, 0, 0, 0);
Posts.find({submitted: {$gt: today}})
Which will return all the posts with a date greater than midnight of today's date.

Try something like this
posts.find({"submitted": /.*today*/})

Related

unable to get previous date using moment.js

I have the following date
let api_date = '2022-03-01T00:00:00.000Z'
Now, i want to get previous 2 dates starting from api_date. Basically now i need dates as 2022-02-28T00:00:00.000Z and 2022-02-27T00:00:00.000Z
Basically for api_date of 1st March, i need previous 2 dates as Feb 28th and Feb 27th.
I tried using this below code
let t-1 = moment().substract(1, 'days')
let t-2 = moment().subtract(2, 'days')
But this only provides previous 2 dates from the present date. i.e. present date is 2nd March, so it provides previous 2 dates based of 2nd March.
how can i use moment to get my current specified date and get previous 2 dates based on that. Any advice to achieve that ? i saw the documentation of moment.js too but i didnt find a definitive answer.
You should create a date object from the date you wanna parse like this,
let api_date = '2022-03-01T00:00:00.000Z'
var dateObj = new Date(api_date);
then you can create a moment object based on the date object just like this,
var momentObj = moment(dateObj);
then if you perfom your specific logic you will get your desired result, like this
let date1 = momentObj.substract(1, 'days')
let date2 = momentObj.substract(2, 'days')
You were almost there, you need to pass your date to moment. Otherwise it defaults to current date ( as you found out ).
let t-1 = moment(api_date).subtract(1, 'days')
let t-2 = moment(api_date).subtract(2, 'days')

How to create a Date object from year, month and date strings?

I'm currently just practising JavaScript and I'm trying to create a programme that calculates how many days there are until your next birthday.
I have seen on this site that there is a daysBetween function that I can use to tell the time difference between two dates (I just need to turn these dates into the millisecond value since the 1st Jan 1970).
The problem is that, although one of those dates is the current date (which is easy to convert into its millisecond value), the second is derived from answers the user inputs as a string into a prompt command (there are three different boxes that ask for the year, month and date of their birth). Is there a way I can convert these input strings into a date format that I can then use to find the days between today's date and their next birthday?
Thanks and sorry if this is a stupid question!
Remember in JS that months are indexed from 0, so January === 0.
var date = new Date('2017','5','25'); would be today, assume you have something like:
var userDd = '25';
var userMm = '6'; // User doesn't know the months are indexed at 0.
var userYy = '2017';
var date = new Date(userYy, userMm - 1, userYy);
date.getTime(); // returns the milliseconds value.

How to get time using Moment JS

I've two javascript variables: startDate and endDate. I want to store the current time(unix timestamp in ms) to endDate and timestamp of 24 hours before in startDate. I'll use this two variables to query the records for last 1 day. How can I do that using moment JS?
Have you looked at the website yet? It's full of examples - http://momentjs.com/ I think what you are trying to do is as simple as
var startDate = moment(endDate).subtract(1, 'days');
Following your question more literally, you can do this:
var endDate = moment(); //the current time
Or, you can just ignore the endDate part of this problem and go straight to startDate with
var startDate = moment().subtract(1, 'days'); //one day before the current time
Finally, if you need to format it a certain way, you can apply a formatting rule such as:
moment().subtract(1,'days').format('YYYY-MM-DD h:mm:ss a')
Use format without an argument and it gives you ISO 8601 format
moment().subtract(1,'days').format() //eg: "2015-04-04T01:53:26-05:00"
This worked for me although I have never found it in the docs. Should have been published but it works.
Try:
moment(currentTime).format("hh:mm"));or
var currentTime = moment();
console.log("CURRENT TIME: " + moment(currentTime).format("hh:mm"));
For those who are looking for a way to get timestamp, just do it:
moment().valueOf()
I think what you are looking for is something like
moment(endDate).unix()
which returns something like:
1435161240
You can even calculate the time from now using
moment(endDate).fromNow()
which returns something like:
"2 days ago"
You can directly call function momentInstance.valueOf(), it will return numeric value of time similar to date.getTime() in native java script.

Invalid Date in Javascript Date

I use an external API to get some data. Then I do some calculations in Javascript with those data. One field is date in this format: 2015-01-26 18:28:14
Then I have to parse this date. I tried with:
var last = "2015-01-26 18:28:14"
var login = new Date(last).getTime();
But I have an error of Invalid Date. I also tried:
var last = "2015-01-26 18:28:14"
var login = Date.parse(last);
You could try insert the character T between the date and the time.
ECMAScript 5 adds support for ISO-8601 dates and times. ISO-8601 stipulates that timestamps with both date and time should be written 2015-01-26T18:28:14.
Note that parse returns:
the number of milliseconds since January 1, 1970, 00:00:00 UTC
See Date.parse() for more info.
Running your code caused errors for me in firefox too
formatting the date like this resolved the issue
"2015/01/26 00:00:00"
var last = "2015/01/26 18:28:14"
var login = new Date(last).getTime();
if you date is coming back with the '-' you can simply do a replace
var d = "2015-01-26 18:28:14";
var login = new Date(d.replace('-', '/')).getTime();

Comparing today's date with another date in moment is returning the wrong date, why?

I'm using moment.js 1.7.0 to try and compare today's date with another date but the diff function is saying they are 1 day apart for some reason.
code:
var releaseDate = moment("2012-09-25");
var now = moment(); //Today is 2012-09-25, same as releaseDate
console.log("RELEASE: " + releaseDate.format("YYYY-MM-DD"));
console.log("NOW: " + now.format("YYYY-MM-DD"));
console.log("DIFF: " + now.diff(releaseDate, 'days'));
console:
RELEASE: 2012-09-25
NOW: 2012-09-25
DIFF: 1
Ideas?
Based on the documentation (and brief testing), moment.js creates wrappers around date objects. The statement:
var now = moment();
creates a "moment" object that at its heart has a new Date object created as if by new Date(), so hours, minutes and seconds will be set to the current time.
The statement:
var releaseDate = moment("2012-09-25");
creates a moment object that at its heart has a new Date object created as if by new Date(2012, 8, 25) where the hours, minutes and seconds will all be set to zero for the local time zone.
moment.diff returns a value based on a the rounded difference in ms between the two dates. To see the full value, pass true as the third parameter:
now.diff(releaseDate, 'days', true)
------------------------------^
So it will depend on the time of day when the code is run and the local time zone whether now.diff(releaseDate, 'days') is zero or one, even when run on the same local date.
If you want to compare just dates, then use:
var now = moment().startOf('day');
which will set the time to 00:00:00 in the local time zone.
RobG's answer is correct for the question, so this answer is just for those searching how to compare dates in momentjs.
I attempted to use startOf('day') like mentioned above:
var compare = moment(dateA).startOf('day') === moment(dateB).startOf('day');
This did not work for me.
I had to use isSame:
var compare = moment(dateA).isSame(dateB, 'day');

Categories