JQDateRangeSlider change start/end value - javascript

I've been working with DateRangeSlider for a couple of days and now I'm using it as a time slider - fiddle
My problem is that the min time is 02:00 and it needs to be 00:00, and max time is 01:59 instead of 23:59.
If I changed the formatter values from
h = val.getHours(),
m = val.getMinutes();
to
h = val.getUTCHours(),
m = val.getUTCMinutes();
it displays the correct start-end hour but when I'm getting those values:
var values = $("#slider").dateRangeSlider('values');
var test = values.min;
it still gets the other values: 02:00 instead of 00:00.
Is there any way to format the min/max value but when the values are read to display the same hour?

You need to take into account the TimeZone and also the Daylight Saving Time (the Date() object is relied on them).
Your solution can be:
var minDateStr = "2014-01-01T00:00:00Z";
var maxDateStr = "2014-01-01T23:59:00Z";
var min2 = new Date(new Date(minDateStr).getTime() + new Date(minDateStr).getTimezoneOffset() * 60000),
max2 = new Date(new Date(maxDateStr).getTime() + new Date(maxDateStr).getTimezoneOffset() * 60000);
Here is a fix to your jsfiddle:
http://jsfiddle.net/z1govrt7/

I have updated format for date and time as below
var min2 = new Date(2014, 0, 1, 00, 00, 00),
max2 = new Date(2014, 11, 31, 23, 59, 59);
Please check http://jsfiddle.net/LJrYf/149/ fiddle now.
I have updated your code and now its working as expected.

Related

setDate(getDate + 30) returns the wrong date

I am trying to implement previous/next two weeks to my scheduler, but it is not working correctly.. This is what I try:
this.datesArray = [];
const currentDate: Date = new Date(this.startDate);
const lastDate: Date = new Date(this.endDate);
currentDate.setDate(currentDate.getDate() - 14);
lastDate.setDate(currentDate.getDate() + 30);
while (currentDate < lastDate) {
// var newDate: Date = new Date(currentDate);
this.datesArray.push(currentDate.toDateString());
currentDate.setDate(currentDate.getDate() + 1);
}
Every click I trigger this code, but there is the following issue. First 2 times it works great, but 3rd time I click next, last date.SetDate bugs out, it addds 60 days instead of 30. Here is what I mean for the previous week:
It shows current date Feb 22, but then adds 30 days, and it shows Apr 21 somehow..
Anyone had similar issues?

Date conversion Local to UTC and UTC to Local in Node.js

i want to change Local time to UTC and vice-versa, by taking users date value.(User will choose the dateand Time and according to dates it will change to UTC and vice versa )
Can someone help on this
If you have your date+time in a JavaScript Date object you can call its getUTCDate()method to get a new Date whose time zone is UTC.
If that is not what you have in mind please be more specific about what you mean by “it will change”.
You can try this:
function calculateTimestamp(date) {
date = date.split('-');
var year = date[2];
var month = date[1];
var day = date[0];
var d1 = new Date(Date.UTC(2017, 9, 1, 17, 0, 0, 0)); //It is static time based upon you will count time in ms
var d2 = new Date(Date.UTC(year, (month - 1), day, 17, 0, 0, 0));
return parseInt((d2.getTime() - d1.getTime()) / 1000);
}
var startDate = '27-02-2018';
var startTime = calculateTimestamp(startDate);
Now you can convert this timestamp again as below:
var d = new Date(Date.UTC(2017, 9, 1, 17, 0, 0, 0));
var t = parseInt(d.getTime() / 1000);
var d = new Date((startTime + t) * 1000);

Calculating how many days between dates, diffrent months [duplicate]

This question already has answers here:
Why does the month argument range from 0 to 11 in JavaScript's Date constructor?
(10 answers)
Closed 5 years ago.
So I'm playing around JavaScript's Date object, and I ran into something I think is a little strange.
I'm trying to figure out how many days there is between 2 given dates, and for that I use the formula below:
var oneDay = 24*60*60*1000;
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
If you take 2017-05-28 & 2017-05-30 it returns 2 days - as it should
var oneDay = 24*60*60*1000;
var firstDate = new Date(2017, 05, 28);
var secondDate = new Date(2017, 05, 30);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
If you take 2017-05-30 & 2017-06-01 it returns 1 days - supposed to be 2 days
var oneDay = 24*60*60*1000;
var firstDate = new Date(2017, 05, 28);
var secondDate = new Date(2017, 05, 30);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
If you take 2017-05-30 & 2017-06-01 it returns 3 days - supposed to be 2 days
var oneDay = 24*60*60*1000;
var firstDate = new Date(2017, 11, 29);
var secondDate = new Date(2017, 12, 01);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
I had used 1½ hour trying to figuring out what the problem was - and 10 sec after posting i figure it out.
Problem is, the date object takes:
Jan, as 0
Feb, as 1
...
...
Nov, as 10
Dec, as 11
Please remember that Date constructor allows also values from outside the logical range.
Example: new Date(2017, -2, 30)
Source:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Where Date is called as a constructor with more than one argument, if
values are greater than their logical range (e.g. 13 is provided as
the month value or 70 for the minute value), the adjacent value will
be adjusted. E.g. new Date(2013, 13, 1) is equivalent to new
Date(2014, 1, 1), both create a date for 2014-02-01 (note that the
month is 0-based). Similarly for other values: new Date(2013, 2, 1, 0,
70) is equivalent to new Date(2013, 2, 1, 1, 10) which both create a
date for 2013-03-01T01:10:00.
You can calculate the difference(days) between two dates by using the code below.
var dateOne = new Date(firstDate);
var dateTwo = new Date(secondDate);
var dateDifference = Math.floor((dateTwo - dateOne) / 86400000);
console.log(dateDifference);

How to calculate the number of gaps between two dates in js

I have two date with time:
YY:MM:DD hh:mm
This is the time period
I need to calculate gap and divide it into 'n' equal parts.
In order to build a graph
Pls Help
Because date is actually saved as an integer and only shown as
YY:MM:DD hh:mm
You can actually just take the two date variables and devide them by the n
gap = (date1 - date2)/n
and then you can get the intervals by just adding the gap multiple times
for(var i = 1; i <= n; i++){
newDate[i] = new Date(date2 + gap*i);
}
something like this?
you can operate directly with dates in javascript
var date1 = new Date(2017, 01, 01, 10, 15, 00);
var date2 = new Date(2016, 12, 01, 10, 14, 45);
var dateDiff = new Date(date1-date2); //this will return timestamp
var years = dateDiff.getFullYear() - 1970; //init date always is 1970
var months = dateDiff.getMonth();
var days = dateDiff.getDate();
var minutes = dateDiff.getMinutes();
var seconds = dateDiff.getSeconds();
alert(years + " years.\r " +
months + " months\r" +
days + " days\r" +
minutes + " minutes\r" +
seconds + " seconds");
I would suggest that you try out the momentjs library. It provides powerful functionalities for you to conveniently work with date objects.
For example, given 2 string dates that are properly formatted, you can get the precise difference between the 2 times easily like so:
let time1 = moment("04/09/2013 15:00:00");
let time2 = moment("04/19/2013 18:20:30");
let diffMilliseconds = time1.diff(time2); // gives the time difference in milliseconds
let diffDays = time1.diff(time2, 'days'); // gives the time difference in days
You can use the date object to convert the given time format to timestamp and then find difference between timestamp.
For example:
var date1 = "2017-03-04 11:22:22"
var date2 = "2017-03-04 13:11:42"
var timestamp1 = Date.parse(date1, "YYYY-MM-DD HH:mm:ss")
var timestamp2 = Date.parse(date2, "YYYY-MM-DD HH:mm:ss")
var difference = timestamp2 - timestamp1;
console.log(difference) //in milliseconds
Now you can divide the difference in to n parts and add to timestamp1 to get following timestamp based on difference/n interval.

Percentage between two dates compared to today

I am creating a UI which will contain Milestones and I will be using a progress bar to show this.
I am trying to come up with the percentage we are from the date the milestone is set to be completed vs today's date.
For example if I have a milestone called "Live to Site" that is set to happen on December 1, 2015, and I put the milestone in on Jan 1st, I want to determine the percentage we are from the start date to the end date.
Fiddle: http://jsfiddle.net/zz4c16fx/2/
var start = new Date(2015,0,1), // Jan 1, 2015 - The date we put this milestone in
end = new Date(2015,7,24), // June 24, 2015 - The date the milestone is due
today = new Date(), // April 23, 2015
p = Math.round(100-((end - start) * 100 ) / today) + '%';
// Update the progress bar
$('.bar').css( "width", p ).after().append(p);
That was my attempt at it but either my math is wrong or I am thinking about this incorrectly. The number its returning is 99%. I feel that number should be lower seeing how we have 1.5 months left from today.
My end result is to show a progress bar that show how close we are to the completion date given the start, end and today's date.
Try this:
var start = new Date(2015, 0, 1), // Jan 1, 2015
end = new Date(2015, 7, 24), // August 24, 2015
today = new Date(), // April 23, 2015
p = Math.round(((today - start) / (end - start)) * 100) + '%';
// Update the progress bar
$('.bar').css("width", p).after().append(p);
Demo: http://jsfiddle.net/zz4c16fx/6/
Try this, convert the end and start date to unit timestamp values.
// Convert to unix values timestamp values
var startDate = start.getTime();
var endDate = end.getTime();
var todayDate = today.getTime();
// Get the total possible timestamp value
var total = endDate - startDate;
// Get the current value
var current = todayDate - startDate;
// Get the percentage
var percentage = (current / total) * 100;
alert(percentage);
JsFiddle http://jsfiddle.net/x3snbz2w/2/
To get the percentage of days elapsed thus far between one date and another, you need to calculate the time between start and end, then calculate the time between start and today. Then divide the second number by the first:
var start = new Date(2015,0,1),
end = new Date(2015,7,24),
today = new Date();
var timeBetweenStartAndEnd = (end - start);
var timeBetweenStartAndToday = (today - start);
var p = Math.round(timeBetweenStartAndToday / timeBetweenStartAndEnd * 100);
console.log("Percentage of days elapsed thus far: " + p);

Categories

Resources