How can I set my local date in javaScript - javascript

I'm using this code to get the difference between today date and my data date from database
I notice every day at 3 pm . next day start
( for example if today is 20 . at 3 pm the date will be 21 )
const oneDay = 24 * 60 * 60 * 1000;
var serverDate = new Date(value)
let todayDate = new Date()
const difference = Math.round(Math.abs((todayDate - serverDate) / oneDay));
return difference
How can I set my local date or there is better way to solve this problem

You can get your local time by passing your location with localeString.
var asiaTime = new Date().toLocaleString("en-US", { timeZone: "Asia/Shanghai"
});
asiaTime = new Date(asiaTime);
console.log('Asia time in string: ' + asiaTime.toLocaleString())
console.log('Asia time in Date format', asiaTime)
var aestTime = new Date().toLocaleString("en-US", {timeZone:
"Australia/Brisbane"});
aestTime = new Date(aestTime);
console.log('AEST time: '+aestTime.toLocaleString())
console.log('AEST time in Date format', aestTime)
var indiaTime = new Date().toLocaleString("en-US", {timeZone:
"Asia/Kolkata"});
indiaTime = new Date(indiaTime);
console.log('India time: '+indiaTime.toLocaleString())
console.log('Inida time in Date format', indiaTime)
Hope this Helps . GOOD LUCK.

If you need to find the difference between the current and your DB date run the below code.
var d = new Date();
var currentDate = moment(d.toLocaleString(),'M/D/YYYY');
var systemDate = moment('2/20/2019','M/D/YYYY');
var diffDays = currentDate.diff(systemDate, 'days');
console.log("diffDays",diffDays);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Related

Nodejs create a date with timzone

I am probably making this harder than I need to.
I am using nodejs on the server. The front-end send me the offset.
I need the UTC equivalent of yesterday (or today, last week...), for example, based on offset.
Currently I have:
getYesterday(): DateRange {
const today = new Date();
const fromDate = format(addDays(today, -1), DATE_SERVER_FORMAT);
const toDate = format(today, DATE_SERVER_FORMAT);
return {
fromDate,
toDate
};
}
But this is all based on the server timezone. I need it based on the offset sent from the frontend.
So today needs to be in UTC. So if the offset is 420 (-7) then Yesterday needs to be '2020-05-19 07:00:00.000' to '2020-05-20 07:00:00.000' even if the server is in Guatamala.
My thoughts are to get today's date (not time) in UTC then add (or subtract) the offset. Then use that date to addDays to.
I'd rather not use an additional library.
Gina
I found the answer here: stackoverflow answer
var d = new Date();
d.setUTCHours(0,0,0,0);
console.log(d.toISOString());
Which allows me to create "yesterday's" date range:
getYesterday(offset :number): DateRange {
var today = new Date();
today.setUTCHours(0,0,0,0);
today = addMinutes(today, offset);
const fromDate = addDays(today, -1).toISOString();
const toDate = today.toISOString();
return {
fromDate,
toDate
};
}
var date1 = new Date();
var date2 = new Date();
console.log(date2.toUTCString());
console.log(date1.getUTCDate());
<h1>Date UTC +_ 0000</h1>
<pre>
You can see the date of output
console.log(date1.getUTCDate());
and
console.log(date2.toUTCString());
is Same
So the simple way to get UTC Date and Time is in built in Date API
</pre>
And to Manage with time difference or what we can say offset Use following script
var targetTime = new Date();
var timeZoneFromClient = -7.00;
var tzDifference = timeZoneFromClient * 60 + targetTime.getTimezoneOffset();
//convert the offset to milliseconds
//add to targetTime
//make a new Date
var offsetTime = new Date(targetTime.getTime() + tzDifference * 60 * 1000);
console.log(offsetTime);

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)

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));
}

JavaScript, get date of the next day [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 8 years ago.
I have the following script which returns the next day:
function today(i)
{
var today = new Date();
var dd = today.getDate()+1;
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
today = dd+'/'+mm+'/'+yyyy;
return today;
}
By using this:
today.getDate()+1;
I am getting the next day of the month (for example today would get 16).
My problem is that this could be on the last day of the month, and therefore end up returning 32/4/2014
Is there a way I can get the guaranteed correct date for the next day?
You can use:
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate()+1);
For example, since there are 30 days in April, the following code will output May 1:
var day = new Date('Apr 30, 2000');
console.log(day); // Apr 30 2000
var nextDay = new Date(day);
nextDay.setDate(day.getDate() + 1);
console.log(nextDay); // May 01 2000
See fiddle.
Copy-pasted from here:
Incrementing a date in JavaScript
Three options for you:
Using just JavaScript's Date object (no libraries):
var today = new Date();
var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
One-liner
const tomorrow = new Date(new Date().getTime() + (24 * 60 * 60 * 1000));
Or if you don't mind changing the date in place (rather than creating
a new date):
var dt = new Date();
dt.setTime(dt.getTime() + (24 * 60 * 60 * 1000));
Edit: See also Jigar's answer and David's comment below: var tomorrow
= new Date(); tomorrow.setDate(tomorrow.getDate() + 1);
Using MomentJS:
var today = moment();
var tomorrow = moment(today).add(1, 'days');
(Beware that add modifies the instance you call it on, rather than
returning a new instance, so today.add(1, 'days') would modify today.
That's why we start with a cloning op on var tomorrow = ....)
Using DateJS, but it hasn't been updated in a long time:
var today = new Date(); // Or Date.today()
var tomorrow = today.add(1).day();
Using Date object guarantees that. For eg if you try to create April 31st :
new Date(2014,3,31) // Thu May 01 2014 00:00:00
Please note that it's zero indexed, so Jan. is 0, Feb. is 1 etc.

Get date time for a specific time zone using JavaScript

It seems that JavaScript's Date() function can only return local date and time. Is there anyway to get time for a specific time zone, e.g., GMT-9?
Combining #​Esailija and #D3mon-1stVFW, I figured it out: you need to have two time zone offset, one for local time and one for destination time, here is the working code:
var today = new Date();
var localoffset = -(today.getTimezoneOffset()/60);
var destoffset = -4;
var offset = destoffset-localoffset;
var d = new Date( new Date().getTime() + offset * 3600 * 1000)
An example is here: http://jsfiddle.net/BBzyN/3/
var offset = -8;
new Date( new Date().getTime() + offset * 3600 * 1000).toUTCString().replace( / GMT$/, "" )
"Wed, 20 Jun 2012 08:55:20"
<script>
var offset = -8;
document.write(
new Date(
new Date().getTime() + offset * 3600 * 1000
).toUTCString().replace( / GMT$/, "" )
);
</script>
You can do this in one line:
let d = new Date(new Date().toLocaleString("en-US", {timeZone: "timezone id"})); // timezone ex: Asia/Jerusalem
var today = new Date();
var offset = -(today.getTimezoneOffset()/60);
You can always get GMT time (so long as the client's clock is correct).
To display a date in an arbitrary time-zone, construct a string from the UTC hours, minutes, and seconds after adding the offset.
There is simple library for working on timezones easily called TimezoneJS can be found at https://github.com/mde/timezone-js.

Categories

Resources