how to use Date.toLocaleString() with changing timezones - javascript

I converted my start and end date to .ToLocalString and now I am trying to use math.abs to calculate difference between start and end date in numbers but its Value is NaN. Any suggestions on how to apply Math.abs in this situation are appreciated.
Note: Start date will be in EDT and EndDate will be in EST. But they
might or might not be in same timezone.
var startDate1 = new Date(homeCtrl.createStartDate);
var startDate = startDate1.toLocaleString();
var endDate1 = new Date(homeCtrl.createEndDate);
var endDate = endDate1.toLocaleString();
var timeDiff = Math.abs(endDate - startDate); //This is NaN
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); // Here it will add 1 extra day. Example: 11/06/2018 - 11/04/2018 = 2 days but this gives 3days are timezone change on 11/04/2018 and thats the issue.

I would use the unix timestamp.
var startDate1 = new Date(homeCtrl.createStartDate);
var startDate = startDate1.getTime();
var endDate1 = new Date(homeCtrl.createEndDate);
var endDate = endDate1.getTime();
var timeDiff = Math.abs(endDate - startDate);

Related

How to get date after n number of days in a date range?

Suppose I have a start date which is 3/Sep/2019 and end date 10/Sep/2019.
I want to get the date after 4 days from the starting date. So if my starting date is 3/sep/2019 I want to get 7/Sep/2019 but not 12/sep/2019 since this date comes after my end date.
How can I achieve this?
So far I'm getting dates after n number of dates like this:
var days = 7;
var date = new Date();
var res = date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
date = new Date(res);
alert(date);
var days = 7;
var date = new Date();
var res = date.setDate(date.getDate() + days);
This is how you get a day 7 days after the date you already have. It wraps to the next month when appropriate as well.
Try the function I wrote below:
function getFutureDate(daysAhead) {
const date = new Date();
date.setDate(date.getDate() + daysAhead);
return date
}
const fourDays = getFutureDate(4)
console.log(fourDays)

Calculate the day difference between two time

I have two date format and I need to calculate the hour difference between both.
First date is current date.
Second Date is in below format :
var depDate = 25092018 //ddmmyyyy
var depTime = 08:35 //hh:mm
new Date function doesn't take date in format ddmmyyyy. How can input depDate in new Date function
I want to check the difference between current date and depDate.
Can anybody help ?
Your date in variable depDate hasn't valid format. You need to validate format of it using regex in .replace(). Then use Date.prototype.getTime() getting numeric value of date.
var depDate = "25092018";
var depTime = "08:35";
var dateStr = depDate.replace(/(\d{2})(\d{2})(\d{4})/, "$3-$2-$1")+"T"+depTime;
// dateStr => 2018-09-25T08:35
var timeDiff = Math.abs(new Date(dateStr).getTime() - new Date().getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
// 1000 => to converting millisecond to second
// 3600 => to converting second to hour
// 24 => to converting hour to day
console.log(diffDays);
If you are using moment module, it would be much easy. Code a below:
let moment = require('moment');
let depDate = "27092018" //ddmmyyyy
let depTime = "08:35" //hh:mm
let date = moment(depDate, "DDMMYYYY").add(moment(depTime,"HH:mm"))
let difference = moment.duration(moment().diff(date)).asHours();
console.log(difference); // This would be the difference in hours.

Javascript - Find out if date is between two dates, ignoring year

I need to find out if my date is between two dates (for checking birthday whether its between +/- 10 days of current date) without taking care of year (because for birthday we don't need year).
I have tried the following but its typical match and will not ignore year. If i ll compare only date and month then overlap on month end makes problems.
(moment(new Date()).isBetween(moment(date).add(10, 'days'), moment(date).subtract(10, 'days')));
Here is the solution that i was end up with.
const birthDate= new Date(birthDate);
birthDate.setFullYear(new Date().getFullYear());
const isBirthdayAround = Math.abs(birthday - new Date) < 10*24*60*60*1000;
And if you are using moment then:
const birthDate= new Date(birthDate);
birthDate.setFullYear(new Date().getFullYear());
const isBirthdayAround = moment(new Date()).isBetween(moment(birthDate).subtract(10, 'days'), moment(birthDate).add(10, 'days'));
if(Math.abs(birthday - new Date) < 10/*d*/ * 24/*h*/ * 60/*min*/ * 60/*secs*/ * 1000/*ms*/)
alert("somewhat in the range");
You can just work with dates as if they were milliseconds. Just get the difference by subtracting them, then check if its smaller than 10 days in milliseconds.
You can use momentjs with methods subtract and add to find any date you want.
Example:
moment().add(7, 'days'); // next 7 days
moment().subtract(7, 'days'); // 7 days ago
This may be help you.
var birthDate = new Date("05/16/1993");
var day = birthDate.getDate();
var month = birthDate.getMonth();
var currentDate = new Date();
var tempDate = new Date();
var oneDay = 1000 * 60 * 60 * 24
var dayDifference = 10 // you can set here difference
tempDate = new Date(tempDate.setMonth(month,day))
var timeDiff = tempDate.getTime() - currentDate.getTime();
timeDiff = Math.round(timeDiff / oneDay)
if(-dayDifference <= timeDiff && timeDiff <=dayDifference){
alert("matched")
}
else{
alert("not matched")
}

JavaScript : How do i set plus or minus 60 days daterange from today?

I need to pass startdate and enddate parameters to a webservice to fetch the response which range is from plus or minus 60 days from the current date. So what value i need to pass it in the StartDate and EndDate param of javascript? My Client Side App is build on JavaScript/AngularJs.
You can do this:
var todaysDate = new Date();
var startDate = new Date();
var endDate = new Date();
startDate.setDate(todaysDate.getDate() - 60);
endDate.setDate(todaysdate.getDate() + 60);
have a look at momentjs http://momentjs.com/ this is an excellent angular wrapper for moment https://github.com/gdi2290/angular-momentjs. always worth adding to your project when dealing with dates and times
Unix timestamp version:
var currentUTC = new Date().valueOf()
var sixtyDays = 60*24*60*60*1000
var startDate = currentUTC - sixtyDays
var endDate = currentUTC + sixtyDays

Decrement the date by one day using javascript for loop?

$(document).ready(function() {
var date = new Date();
var data_new = [];var url ='http://www.domain.com /kjdshlka/api.php?date=2014-07-15';
$.getJSON(url,function(result) {
var elt = [date,result.requests];data_new.push(elt);console.log(data_new);
});
});
I am struggling to decrement the date by one day using javascript for loop.Here is my code,from the url im getting some requests.like if i decrease the date by one day other requests will come .Now i need this process for 7days using javascript for loop.Can anybody please tel me how to do ?
var date = new Date(); // Date you want, here I got the current date and time
date.setDate(date.getDate()-1);
getDate() will give you the date, then reduce it by 1 and using setDate() you can replace date again.
var today = new Date();
var yesterday = new Date(today.getTime() - (24 * 60 * 60 * 1000)); //(hours * minutes * seconds * milliseconds)
console.log(yesterday);
var now = new Date();
console.log(now);
var yesterday = new Date(now - 86400000);
console.log(yesterday);
/* In a Decrement Loop*/
for(var i=100;i>0;i--){
console.log(new Date(now - i*86400000));
}

Categories

Resources