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);
Related
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>
I'm looking for the fastest method to build a timestamp which represents the current hour starting from the current instant (in general, starting from a timestamp)
Currently I'm doing the following:
var d = new Date();
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();
var hour = d.getUTCHours();
d = new Date(year, month, day, hour);
console.log(d);
console.log(d.getTime());
Is it possible to avoid the second invocation of Date?
If I understand you correctly you want the timestamp of the beginning of the current hour. Then you could simply set the minutes and seconds to 0 in your first Date Object:
var d = new Date();
d.setMinutes(0,0);
console.log(d);
console.log(d.getTime());
You could make it a oneliner, since setMinutes() already returns a timestamp:
var timestamp = new Date().setMinutes(0,0);
Not sure why you're doing that twice, you really don't need to.
var d = new Date();
console.log(d.getHours());
// or
console.log(d.getTime() );
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
I have a datetime in ISO format i.e.
2012-06-26T01:00:44Z
I want to get the time difference from current time. How can I achieve this using javascript or javascript library Date.js or jquery
This will give you the difference in milliseconds, you can then format it as you want
var diff = new Date("2012-06-26T01:00:44Z") - new Date();
Try this:
var someDate = new Date("2012-06-26T01:00:44Z");
var now = new Date();
var one_day = 1000 * 60 * 60 * 24;
var diff = Math.ceil((someDate.getTime()-now .getTime())/(one_day))
alert(diff)
Example fiddle
You can obviously amend the one_day variable to get the difference in the unit you require.
I would suggest converting ISO format to something that works cross browser.
Try this,
var d = "2012-06-26T01:00:44Z";
var someDate = new Date(d.replace(/-/g,'/').replace('T',' ').replace('Z',''));
alert(someDate - new Date());
Edit:
I guess, you need pretty time
Try this awesome code
Edit 2:
You needed reverse, so try this instead
var old_date = new Date();
alert('Old date: ' + old_date.toGMTString())
var new_date = new Date(old_date.setMinutes(old_date.getMinutes() - 5));
alert('Date 5 minutes before: ' + new_date.toGMTString());
If you need timestamp,
alert(new_date.getTime());
in order to format date you can use this function to get the desire format of the date and you can easily change the position of day , month and year.
function convertFormat(inputDate)
var date = new Date(inputDate);
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear();
var fullYear = year + '/' + month + '/' + day
return fullYear;
I have a string that contains 8 digits that represent a date. For example:
20120515
I'd like to compare it with today's date, created in this manner:
var currentDate = new Date();
How can I convert the "8 digit date string" to a suitable date format in order to compare it to currentDate?
Use the substring method and substring off 4 elements and assign it to your new date for the year. Then substring off two elements at a time and store the month and date accordingly.
var dateString = "20120515";
var year = dateString.substring(0,4);
var month = dateString.substring(4,6);
var day = dateString.substring(6,8);
var date = new Date(year, month-1, day);
var currentDate = new Date();
Now you can compare the two dates with the normal operators.
If you want a small date library you can use moment.js.
var a = moment("20120515", "YYYYMMDD");
// then use any of moment's manipulation or display functionality
a.format("MMM Do YYYY"); // May 15th 2012
a.fromNow(); // 14 hours ago
a.calendar(); // Today at 12:00 AM
To correctly handle the local time zone, it must explicitly summed to the calculated time
function dateStringToDate(dateString) {
try {
var year = dateString.substring(0, 4);
var month = dateString.substring(4, 6);
var day = dateString.substring(6, 8);
var date = new Date(year, month - 1, day);
const offset = date.getTimezoneOffset()
date = new Date(date.getTime() - (offset * 60 * 1000));
return date;
} catch (error) {
return null;
}
}
function dateStringToDate(dateString) {
try {
var year = dateString.substring(0, 4);
var month = dateString.substring(4, 6);
var day = dateString.substring(6, 8);
var date = new Date(year, month - 1, day);
const offset = date.getTimezoneOffset()
date = new Date(date.getTime() - (offset * 60 * 1000));
return date;
} catch (error) {
return null;
}
}
console.log(dateStringToDate("20211212"))
console.log(dateStringToDate("20211213"))
console.log(dateStringToDate("20211214"))
...some other "one-liner" ways to accomplish this:
(They take a value like dts='20020704'; and return date object [dt].)
var dt=new Date(dts.slice(0,4), (dts[4]+dts[5])-1, dts[6]+dts[7]);
...or...
var m=dts.match(/(....)(..)(..)/), dt=new Date(m[1],m[2]-1,m[3]);
...or...
var m=dts.match(/.{1,2}/g), dt=new Date(m[0]+m[1],m[2]-1,m[3]);
The last one's shortest, but the first is probably most efficient, since it doesn't use regex (but that's irrelevant, unless you're processing LOTS of data using this). I like the middle one best since it's easy to see what's happening.