I need to calculate the difference between two dates in days
i bring today date: new Date().toJSON().slice(0, 10) = 2019-04-17
and the other date in the same form
var msDiff = new Date("June 30, 2035").getTime() - new Date().getTime(); //Future date - current date
var daysTill30June2035 = Math.floor(msDiff / (1000 * 60 * 60 * 24));
console.log(daysTill30June2035);
You can implement it yourself, but why would you? The solution to that already exists, and somebody else has taken care (and still is taking care) that it works as it should.
Use date-fns.
import differenceInDays from 'date-fns/difference_in_days';
If you really want to bash your head, you can get difference in milliseconds and then divide by number of milliseconds in a day. Sounds good to me, but I'm not 100% sure if it works properly.
const differenceInDays = (a, b) => Math.floor(
(a.getTime() - b.getTime()) / (1000 * 60 * 60 * 24)
)
If you manipulate many dates, maybe an external library like moment.js could be useful. There are multiple add-ons like the date range one.
Once installed, you need to create a range
const start = new Date(2011, 2, 5);
const end = new Date(2011, 5, 5);
const range = moment.range(start, end);
Then could get the difference by doing something like
range.diff('months'); // 3
range.diff('days'); // 92
range.diff(); // 7945200000
Hope it could be useful :)
var d1 = new Date("2019/04/17") //firstDate
var d2 = new Date("2011/02/01") //SecondDate
var diff = Math.abs(d1-d2); //in milliseconds
Use differenceInDays, parseISO from "date-fns"
import DateTimePicker from "#react-native-community/datetimepicker";
import { differenceInDays, parseISO } from "date-fns";
let day = differenceInDays(
parseISO(your end date),
parseISO(yout first Date)
);
console.log("differenceInDays",differenceInDays)
Related
let start = moment(el.StartDate).format('HH:mm')
not able to change the 24 hours format in react-big-calender please help me
let start = moment(el.StartDate).format("hh:mm a")
this should work.
but In case it didn't work here is another solution.
This assumes s is either 3 characters (930) or 4 characters (2130) but does absolutely no other validation.
function militaryTimeTo12Hour(s) {
if(s.length == 3) s = `0${s}`; // 930 -> 0930
const hour = parseInt(s.substring(0, 2), 10);
const min = parseInt(s.substring(2, 4), 10);
if(hour < 12) return `${hour % 12}:${min} AM`;
return `${hour % 12 || 12}:${min} PM`;
}
resource : https://stackoverflow.com/a/68575347/6654562
I am trying to implement a method for a Date selection in vue-chartjs. Here is the function that i have used in the methods life cycle hook:
DateSelect(event) {
const period = event.target.value
let minValue = new Date(Math.max(...this.date) * 1000)
const axisXMin = new Date(Math.min(...this.date) * 1000)
switch (period) {
case '1m':
minValue.setMonth(minValue.getMonth() - 1)
break
case '3m':
minValue.setMonth(minValue.getMonth() - 3)
break
case 'ytd':
minValue.setFullYear(minValue.getFullYear() - minValue.getMonth()) //Here I want to implement the YTD Logic.
break
default:
minValue = axisXMin
}
const data = this.data.filter(el => {
return el.x >= minValue
})
this.GraphOutput(data) // this is vue-chartjs function.
}
Here the logic '1m' and '3m' works absolutely fine, as they display the previous 1month's and 3month's chart to the user when the respective button is clicked.
I want to know how to implement the YTD (Year to Date) Logic in the function that i have used above. Please do help me.
As far I understand you may need a full year of data in the graph. So that you need to set the minimum value as below:
case 'ytd':
minValue.setYear(minValue.getFullYear() - 1);
break
Moment JS is returning a random date.
I am trying to create a calendar in a react-native project. I created this loop to count back the days from today's date. I have attached my console and tried to clearly show how I tried to debug this.
The subtract method seems not to recognise the loop and the i value seems to stay at 16.
Any help would be greatly appreciated.
This is the code that is causing all the fussThis is the output in the console
Per Moment.js, moments are mutable. Clone the moment before performing date math
https://momentjs.com/guides/#/lib-concepts/mutability/
You'll need to create a new moment instance or clone the existing one when performing operations like .add(), .subtract(), etc.
const moment = require("moment");
const today = moment();
// count back N days
const N = 3;
let i = 1;
let days = [];
// #1 - Create a new moment instance from today and add -i days
while (i <= N) {
const day = moment(today).add(-i, "days");
days.push(day);
i++;
}
console.log("Approach #1");
console.log({ days });
// #2 - A moment is mutable. So we use add() to mutate today by -1 days each iteration, clone the instance, and push to array
i = 0;
days = [];
while (i < N) {
const day = today.add(-1, "days").clone();
days.push(day);
i++;
}
console.log("Approach #2");
console.log({ days });
I need to create a unit test for the following function:
export default date => {
const dateInMs = new Date(date).getTime(); // UTC date
const clientDate = new Date();
// timezone offset is in minutes therefore it is being converted to milliseconds
const offset = clientDate.getTimezoneOffset() * 60 * 1000;
return new Date(dateInMs - offset).toISOString();
};
I know I should pass the test a date in the following format
2017-07-01T00:00:00+00:00
Can someone provide some guidance as to how to write a simple test with this information.
I have a spin edit from Devexpress and what I would like to do with it is set its value to only display a year month value, with the format or YYYYMM. I have code to do that but I was wondering if it is possible to do this with the properties of the spin edit. The problem I encounter is that when it gets to 201212 and you increment it should go to 201301, but of cause it just goes to 201213. The same happens in revers from 201301 to 201299. I'll give the sample code just so that you can see how it is meant to work but again I want to know if this can be done using just the properties of the spinEdit.
private void spDate_EditValueChanged(object sender, EventArgs e)
{
spDate.Properties.EditValueChangedFiringMode = DevExpress.XtraEditors.Controls.EditValueChangedFiringMode.Default;
int year = Convert.ToInt32(Math.Floor(spDate.Value / 100));
int month = Convert.ToInt32(spDate.Value - (year * 100));
if (month > 12) { month = 1; year += 1; }
if (month < 1) { month = 12; year -= 1; }
spDate.Value = (year * 100) + (month);
}
obviously that first line is manually set with the properties otherwise its a little buggy but I added it in there so you can see what property to set beforehand.
Many thanks
I Think that you have to use editors masks. What you are trying to display is obviously a date. Try to have a look here.