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')
Related
I am making a filter between two dates, Start date and End date, the filter works perfect, it brings the data but it does not bring the complete data and it is because when selecting the dates and converting them to the format, it converts them but one day remains.
This way I am converting the dates:
FiltrarPorFechas(incial, final) {
this.ListaUsuarios = [];
const IniDate = new Date(incial);
const EndDate = new Date(final);
}
associate an image with debug and conversion:
As shown in the image, the initial and final dates arrive at the method thus "2019-07-10" and "2019-07-31" but when I try to convert them it puts them one day less as shown in the image.
I have tried to use moment formatDate and it does not work, I do not understand why and I do not want to add one day.
Somebody could help me ?
You may be potentially having an issue with Timezone. You could reset the time to 00:00:00 either using moment.js or from a normal Date() object.
Now, since your aim is to compare the times, you can use the diff() available from moment.js to achieve this. Please find the sample code below
(function() {
initial = '2019-07-09';
initial_formatted = moment(new Date(`${initial} 00:00:00`));
final = '2019-07-31';
final_formatterd = moment(new Date(`${final} 00:00:00`));
console.log(initial_formatted.diff(final_formatterd, 'days'));
// moment(new Date(`${incial} 00:00:00`)).format('YYYY-MM-DD HH:mm:ss');
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
More info
Moment diff()
Date Object set() method
I'm trying to add days to a Date object, but the output is not as desired:
// THIS IS JUST A SIMPLIFIED EXAMPLE.
let date = new Date("2019-01-01 00:00:00")
let finalDate = new Date()
finalDate.setDate(date.getDate() + 10)
console.log(finalDate)
Desired output:
11/01/2019 00:00:00
Actual output:
31/08/2019 13:06:30
It's using the current system date as a base and setting it to finalDate. which is not what I'm looking for.
The way you were declaring your literal date was in error. Also, you're better passing the existing date as a parameter to the constructor for the second one.
let date = new Date("2019-01-01 00:00:00");
let finalDate = new Date(date);
finalDate.setDate(date.getDate() + 10);
console.log(finalDate)
If your desired output is 11/01/2019 then you need to change a few things in how you're calculating your dates.
Here's code that get you what you're looking for:
let date = new Date('2019/01/01 00:00:00');
let finalDate = date;
finalDate.setMonth(date.getMonth() + 10);
console.log(finalDate);
Notice that for finalDate, I'm not setting it to a new instance of a date, but rather assigning it the value of the date variable. This way the two are the exact same date and allows us to begin adding months to the one we wish to add months to. Otherwise the days may not come out the same by initializing finalDate as its own separate date object.
Also notice that I'm calling getMonth rather than getDate, since we're adding months strictly.
Here is a working jsfiddle of your desired results:
https://jsfiddle.net/yzmk61xf/#&togetherjs=sSETlrppq6
I have a date string in variable called dateEnd like this Mon Nov 20 2017 23:59:59 GMT-0200 on my react component and I want to extract the day (20) and convert it to a number.
This is my code:
let dateEnd = rangePicker['endDate'] && rangePicker['endDate'].toString();
How can I do this?
I've tried some like this:
let dateEndNum = parseInt(dateEnd.replace(/^\D+|\D.*$/g, ""), 10);
But returns me a error, because .replace is not defined.
In addition, I want to get the initial date and the end date to calculate how many days has between those dates.
This would return a boolean.
let dateEnd = rangePicker['endDate'] && rangePicker['endDate'].toString();
What you want is something like this:
let dateEnd = rangePicker['endDate'] ? rangePicker['endDate'].toString() : '';
Your issue is not related with react. However I suggest you try out moment.js - it is a super powerful js library for handling datetime objects. In your case:
const momentDate = moment("Mon Nov 20 2017 23:59:59 GMT-0200");
const extractedDays = momentDate.date();
Although this isn't related to React, I recommend using momentjs, you can create a moment object using the date string, and one of the many functions can get you want you want.
Following is the type of date I am getting from db - 2015-07-15T18:30:00.000Z . Now I am trying to add bootstap class btn-info if this date is 1 year before current date (Date.now()), if its less than or equals to 1 year but less than 3 months from current date then add class btn-warning, for 3 months or less add class btn-danger.
For this I am using ng-class on my field, like -
ng-class="calDateDiff(exp_date)"
In calDateDiff() method, I am trying to do the conversion, but unable to know which type of date format its represents.
I checked UTC in Js but its not that, also I tried Date.parse(Date.now()) but unable to know the format.
$scope.calDatediff = function(exp_date) {
// exp_date - Date.now()
// Here in calculation I am also not sure
// to handle leap years
}
Please let me know what type of date format is this and what is the right approach to handle this.
FYI- I don't want to use Moment.js as this is to be applied for only one column field, so it will be overhead just for one column to show.
EDIT 1 -
Ok I got this as its an ISO string -
so for current date I believe its going to be -
current date - new Date().toISOString()
let me know how I cam going to compare these two strings for an year, or 3 month condition ?
You can create a new date object and subtract.
Date.now() - new Date('2015-07-15T18:30:00.000Z')
1557113984
if you want to know if a year has elapsed one way to do it is to increment the date you got from the database and see if it is less than the current date:
var dbDate = new Date('2015-07-15T18:30:00.000Z');
dbDate
Wed Jul 15 2015 11:30:00 GMT-0700 (PDT)
dbDate.setMonth(dbDate.getMonth() + 12);
1468607400000
dbDate
Fri Jul 15 2016 11:30:00 GMT-0700 (PDT)
dbDate < Date.now()
false
That seems to me like an ISOString date. All you need to do is create a new object of type Date by passing that string to the constructor:
var dbDate = new Date("2015-07-15T18:30:00.000Z");
This will give you a Date object with which you can work with and easily compare it to Date.now(); I think you can do the rest of it by yourself.
Here's more info about Date: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date
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*/})