Javascript -- Unix Time to Specific Time Zone - javascript

I'm working with a service that gives me broadcast times for Television shows in Unix Time (seconds since midnight, January 1st, 1970, in Greenwich, England). I need to convert this, in javascript, to Eastern Standard time (USA). I need to account for daylight savings time, and for the fact that the client's clock may be set to something other than Eastern Standard time. I'm sure this code has been written before. Can anyone point me toward it?

What you'll find is it's not possible to translate to a specific timezone, but as long as your users are in the desired timezone, this will work:
var date = new Date();
date.setTime(unixTime * 1000);
The resulting date object will display in the timezone of the computer running the browser:
window.console.log(date.toString())
yields:
"Thu Jun 25 2009 09:48:53 GMT-0400 (EDT)"
for me anyway)

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html
Looks to have a solution for changing timezones, but it does look like you have to do the math yourself. There is no, setTimezone or setLocale method.

I wrote some code which will turn GMT milliseconds into a Date-like object which can be queried for eastern standard time values. It handles daylight savings.
ESTDate = function(millis){
if(isNaN(parseInt(millis))) {
throw new Error("ESTDate must be built using a number");
}
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
var gmtDate = new Date(millis);
var clockSetDate = function(month){
var date = new Date(0);
date.setUTCFullYear(gmtDate.getUTCFullYear());
date.setUTCMonth(month);
date.setUTCHours(2);
while(date.getUTCDay() != "0"){
date.setTime(
date.getTime() + MILLIS_PER_DAY
);
};
return date;
}
var startStandarTimeDate = clockSetDate(2);
var endStandardTimeDate = clockSetDate(10);
date = new Date(millis);
var estOffset = 60 * 60 * 1000 * 4;
var dltOffset = (
(startStandarTimeDate < date) &&
(date < endStandardTimeDate)
) ? 0: 60 * 60 * 1000;
date.setTime(date.getTime() - (estOffset + dltOffset));
var self = {
getDate: function(){
return date.getUTCDate();
},
getDay:function(){
return date.getUTCDay();
},
getFullYear:function(){
return date.getUTCFullYear();
},
getHours:function(){
return date.getUTCHours();
},
getMilliseconds:function(){
return date.getUTCMilliseconds();
},
getMinutes:function(){
return date.getUTCMinutes();
},
getMonth:function(){
return date.getUTCMonth();
},
getSeconds:function(){
return date.getUTCSeconds();
}
}
return self;
}

Related

Add time to datetime and return the date and time javascript [duplicate]

I'd like to get a Date object which is 30 minutes later than another Date object. How do I do it with JavaScript?
Using a Library
If you are doing a lot of date work, you may want to look into JavaScript date libraries like Datejs or Moment.js. For example, with Moment.js, this is simply:
var newDateObj = moment(oldDateObj).add(30, 'm').toDate();
Vanilla Javascript
This is like chaos's answer, but in one line:
var newDateObj = new Date(oldDateObj.getTime() + diff*60000);
Where diff is the difference in minutes you want from oldDateObj's time. It can even be negative.
Or as a reusable function, if you need to do this in multiple places:
function addMinutes(date, minutes) {
return new Date(date.getTime() + minutes*60000);
}
And just in case this is not obvious, the reason we multiply minutes by 60000 is to convert minutes to milliseconds.
Be Careful with Vanilla Javascript. Dates Are Hard!
You may think you can add 24 hours to a date to get tomorrow's date, right? Wrong!
addMinutes(myDate, 60*24); //DO NOT DO THIS
It turns out, if the user observes daylight saving time, a day is not necessarily 24 hours long. There is one day a year that is only 23 hours long, and one day a year that is 25 hours long. For example, in most of the United States and Canada, 24 hours after midnight, Nov 2, 2014, is still Nov 2:
const NOV = 10; //because JS months are off by one...
addMinutes(new Date(2014, NOV, 2), 60*24); //In USA, prints 11pm on Nov 2, not 12am Nov 3!
This is why using one of the afore-mentioned libraries is a safer bet if you have to do a lot of work with this.
Below is a more generic version of this function that I wrote. I'd still recommend using a library, but that may be overkill/impossible for your project. The syntax is modeled after MySQL DATE_ADD function.
/**
* Adds time to a date. Modelled after MySQL DATE_ADD function.
* Example: dateAdd(new Date(), 'minute', 30) //returns 30 minutes from now.
* https://stackoverflow.com/a/1214753/18511
*
* #param date Date to start with
* #param interval One of: year, quarter, month, week, day, hour, minute, second
* #param units Number of units of the given interval to add.
*/
function dateAdd(date, interval, units) {
if(!(date instanceof Date))
return undefined;
var ret = new Date(date); //don't change original date
var checkRollover = function() { if(ret.getDate() != date.getDate()) ret.setDate(0);};
switch(String(interval).toLowerCase()) {
case 'year' : ret.setFullYear(ret.getFullYear() + units); checkRollover(); break;
case 'quarter': ret.setMonth(ret.getMonth() + 3*units); checkRollover(); break;
case 'month' : ret.setMonth(ret.getMonth() + units); checkRollover(); break;
case 'week' : ret.setDate(ret.getDate() + 7*units); break;
case 'day' : ret.setDate(ret.getDate() + units); break;
case 'hour' : ret.setTime(ret.getTime() + units*3600000); break;
case 'minute' : ret.setTime(ret.getTime() + units*60000); break;
case 'second' : ret.setTime(ret.getTime() + units*1000); break;
default : ret = undefined; break;
}
return ret;
}
Working jsFiddle demo.
var d1 = new Date (),
d2 = new Date ( d1 );
d2.setMinutes ( d1.getMinutes() + 30 );
alert ( d2 );
var oldDateObj = new Date();
var newDateObj = new Date();
newDateObj.setTime(oldDateObj.getTime() + (30 * 60 * 1000));
console.log(newDateObj);
var now = new Date();
now.setMinutes(now.getMinutes() + 30); // timestamp
now = new Date(now); // Date object
console.log(now);
Maybe something like this?
var d = new Date();
var v = new Date();
v.setMinutes(d.getMinutes()+30);
console.log(v)
I always create 7 functions, to work with date in JS:
addSeconds, addMinutes, addHours, addDays, addWeeks, addMonths, addYears.
You can see an example here: http://jsfiddle.net/tiagoajacobi/YHA8x/
How to use:
var now = new Date();
console.log(now.addMinutes(30));
console.log(now.addWeeks(3));
These are the functions:
Date.prototype.addSeconds = function(seconds) {
this.setSeconds(this.getSeconds() + seconds);
return this;
};
Date.prototype.addMinutes = function(minutes) {
this.setMinutes(this.getMinutes() + minutes);
return this;
};
Date.prototype.addHours = function(hours) {
this.setHours(this.getHours() + hours);
return this;
};
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addWeeks = function(weeks) {
this.addDays(weeks*7);
return this;
};
Date.prototype.addMonths = function (months) {
var dt = this.getDate();
this.setMonth(this.getMonth() + months);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
Date.prototype.addYears = function(years) {
var dt = this.getDate();
this.setFullYear(this.getFullYear() + years);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
One line code
var afterSomeMinutes = new Date(new Date().getTime() + minutes * 60000);
where minutes is a number
Stop using Moment.js
As recommended by other great answers, in most cases it's best to use a library when dealing dates. However, it's important to know that as of September 2020 Moment.js is considered legacy and should no longer be used in new projects.
Quoting Moment's statement in their official docs:
We would like to discourage Moment from being used in new projects going forward. [...] We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.
Modern libraries
Below are alternatives recommended by Moment.
Luxon
Luxon can be thought of as the evolution of Moment. It is authored by Isaac Cambron, a long-time contributor to Moment. Please read Why does Luxon exist? and the For Moment users pages in the Luxon documentation.
Locales: Intl provided
Time Zones: Intl provided
import {DateTime} from 'luxon'
function addMinutes(date, minutes) {
return DateTime.fromJSDate(date).plus({minutes}).toJSDate()
}
Day.js
Day.js is designed to be a minimalist replacement for Moment.js, using a similar API. It is not a drop-in replacement, but if you are used to using Moment's API and want to get moving quickly, consider using Day.js.
Locales: Custom data files that can be individually imported
Time Zones: Intl provided, via a plugin
import dayjs from 'dayjs'
function addMinutes(date, minutes) {
return dayjs(date).add(minutes, 'minutes').toDate()
}
date-fns
Date-fns offers a series of functions for manipulating JavaScript Date objects. For more details, scroll to "Why date-fns?" on the date-fns home page.
Locales: Custom data files that can be individually imported
Time Zones: Intl provided, via a separate companion library
import {addMinutes} from 'date-fns'
function addMinutesDemo(date, minutes) {
return addMinutes(date, minutes)
}
js-Joda
js-Joda is a JavaScript port of Java's Three-Ten Backport, which is the base for JSR-310 implementation of the Java SE 8 java.time package. If you are familiar with java.time, Joda-Time, or Noda Time, you will find js-Joda comparable.
Locales: Custom data files via add-on module
Time Zones: Custom data files via add-on module
import {LocalDateTime, nativeJs, convert} from '#js-joda/core'
function addMinutes(date, minutes) {
return convert(
LocalDateTime.from(
nativeJs(date)
).plusMinutes(minutes)
).toDate()
}
One-liner no utilities:
new Date(+new Date() + 60000*15) // +15 minutes
The easiest way to solve is the to recognize that in javascript dates are just numbers. It starts 0 or 'Wed Dec 31 1969 18:00:00 GMT-0600 (CST). Every 1 represents a millisecond. You can add or subtract milliseconds by getting the value and instantiating a new date using that value. You can manage it pretty easy with that mind.
const minutesToAdjust = 10;
const millisecondsPerMinute = 60000;
const originalDate = new Date('11/20/2017 10:00 AM');
const modifiedDate1 = new Date(originalDate.valueOf() - (minutesToAdjust * millisecondsPerMinute));
const modifiedDate2 = new Date(originalDate.valueOf() + (minutesToAdjust * millisecondsPerMinute));
console.log(originalDate); // Mon Nov 20 2017 10:00:00 GMT-0600 (CST)
console.log(modifiedDate1); // Mon Nov 20 2017 09:50:00 GMT-0600 (CST)
console.log(modifiedDate2); // Mon Nov 20 2017 10:10:00 GMT-0600 (CST)
This is what I do which seems to work quite well:
Date.prototype.addMinutes = function(minutes) {
var copiedDate = new Date(this.getTime());
return new Date(copiedDate.getTime() + minutes * 60000);
}
Then you can just call this like this:
var now = new Date();
console.log(now.addMinutes(50));
You should get the value of the current date to get the date with (ms) and add (30 * 60 *1000) to it. Now you have (current date + 30 min) with ms
console.log('with ms', Date.now() + (30 * 60 * 1000))
console.log('new Date', new Date(Date.now() + (30 * 60 * 1000)))
it is simple as it is;
let initial_date = new Date;
let added30Min = new Date(initial_date.getTime() + (30*60*1000));
Here is the ES6 version:
let getTimeAfter30Mins = () => {
let timeAfter30Mins = new Date();
timeAfter30Mins = new Date(timeAfter30Mins.setMinutes(timeAfter30Mins.getMinutes() + 30));
};
Call it like:
getTimeAfter30Mins();
Here is my one-liner:
console.log('time: ', new Date(new Date().valueOf() + 60000))
I feel many of the answers here are lacking a creative component, very much needed for time travel computations. I present my solution for a temporal translation of 30 minutes.
(jsfiddle here)
function fluxCapacitor(n) {
var delta,sigma=0,beta="ge";
(function(K,z){
(function(a,b,c){
beta=beta+"tT";
switch(b.shift()) {
case'3':return z('0',a,c,b.shift(),1);
case'0':return z('3',a,c,b.pop());
case'5':return z('2',a,c,b[0],1);
case'1':return z('4',a,c,b.shift());
case'2':return z('5',a,c,b.pop());
case'4':return z('1',a,c,b.pop(),1);
}
})(K.pop(),K.pop().split(''),K.pop());
})(n.toString().split(':'),function(b,a,c,b1,gamma){
delta=[c,b+b1,a];sigma+=gamma?3600000:0;
beta=beta+"im";
});
beta=beta+"e";
return new Date (sigma+(new Date( delta.join(':')))[beta]());
}
You could do this:
let thirtyMinutes = 30 * 60 * 1000; // convert 30 minutes to milliseconds
let date1 = new Date();
let date2 = new Date(date1.getTime() + thirtyMinutes);
console.log(date1);
console.log(date2);
Here is the IsoString version:
console.log(new Date(new Date().setMinutes(new Date().getMinutes() - (30))).toISOString());
var add_minutes = function (dt, minutes) {
return new Date(dt.getTime() + minutes*60000);
}
console.log(add_minutes(new Date(2014,10,2), 30).toString());
One way to 'add' 30 minutes is to create a second date object (mostly for demonstration) and set the minutes to minutes + 30. This will account for adjusting the hour as well if the first time is less than 30 minutes from the next hour. (i.e., 4:45 to 5:15)
const first = new Date();
console.log("first date :", first.toString());
const second = new Date(first);
const newMinutes = second.getMinutes() + 30;
console.log("new minutes:", newMinutes);
second.setMinutes(newMinutes);
console.log("second date:", second.toString());
I know that the topic is way too old. But I am pretty sure that there are some developpers who still need this, so I made this simple script for you.
I hope you enjoy it!
Hello back, It's 2020 and I've added some modification hope it will help a lot better now!
Hello again, It is 2022 and I came back again to fix some issues and give a better naming for the methods & functions.
function addTimeToDate(addedTime, date){
let generatedTime = date.getTime();
if(addedTime.seconds) generatedTime += 1000 * addedTime.seconds; //check for additional seconds
if(addedTime.minutes) generatedTime += 1000* 60 * addedTime.minutes;//check for additional minutes
if(addedTime.hours) generatedTime += 1000 * 60 * 60 * addedTime.hours;//check for additional hours
return new Date(generatedTime);
}
Date.prototype.addTime = function(addedTime){
return addTimeToDate(addedTime, this);
}
let futureDate = new Date().addTime({
hours: 16, //Adding one hour
minutes: 45, //Adding fourty five minutes
seconds: 0 //Adding 0 seconds return to not adding any second so we can remove it.
});
<button onclick="console.log(futureDate)">Travel to the future</button>
Use an existing library known to handle the quirks involved in dealing with time calculations. My current favorite is moment.js.
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script>
var now = moment(); // get "now"
console.log(now.toDate()); // show original date
var thirty = moment(now).add(30,"minutes"); // clone "now" object and add 30 minutes, taking into account weirdness like crossing DST boundries or leap-days, -minutes, -seconds.
console.log(thirty.toDate()); // show new date
</script>
Other solution:
var dateAv = new Date();
var endTime = new Date(dateAv.getFullYear(), dateAv.getMonth(), dateAv.getDate(), dateAv.getHours(), dateAv.getMinutes() + 30);
let d = new Date();
d.setMinutes(d.getMinutes() + 30);
// console.log(d)
For the lazy like myself:
Kip's answer (from above) in coffeescript, using an "enum", and operating on the same object:
Date.UNIT =
YEAR: 0
QUARTER: 1
MONTH: 2
WEEK: 3
DAY: 4
HOUR: 5
MINUTE: 6
SECOND: 7
Date::add = (unit, quantity) ->
switch unit
when Date.UNIT.YEAR then #setFullYear(#getFullYear() + quantity)
when Date.UNIT.QUARTER then #setMonth(#getMonth() + (3 * quantity))
when Date.UNIT.MONTH then #setMonth(#getMonth() + quantity)
when Date.UNIT.WEEK then #setDate(#getDate() + (7 * quantity))
when Date.UNIT.DAY then #setDate(#getDate() + quantity)
when Date.UNIT.HOUR then #setTime(#getTime() + (3600000 * quantity))
when Date.UNIT.MINUTE then #setTime(#getTime() + (60000 * quantity))
when Date.UNIT.SECOND then #setTime(#getTime() + (1000 * quantity))
else throw new Error "Unrecognized unit provided"
# # for chaining
Just another option, which I wrote:
DP_DateExtensions Library
It's overkill if this is all the date processing that you need, but it will do what you want.
Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc. It's liberally open sourced.
simply you can use this code with momnet library:
console.log(moment(moment()).add(30,"minutes").format('MM/DD/YYYY hh:mm:ss'));
var myDate= new Date();
var MyNewDate = new Date
(myDate.getFullYear(),myDate.getMonth(),myDate.getDate(),myDate.getMinutes()+10,01,01)
const MINUTE = 60 * 1000;
new Date(Date.parse(yourDate) + numOfMins * MINUTE)

How do I convert UTC/ GMT datetime to CST in Javascript? (not local, CST always)

I have a challenge where backend data is always stored in UTC time. Our front-end data is always presented in CST. I don't have access to this 'black box.'
I would like to mirror this in our data warehouse. Which is based in Europe (CET). So "local" conversion will not work.
I'm wondering the simplest, most straightforward way to accurately convert UTC time (I can have it in epoch milliseconds or a date format '2015-01-01 00:00:00') to Central Standard Time. (which is 5 or 6 hours behind based on Daylight Savings).
I see a lot of threads about converting to 'local' time ... again I don't want this, nor do I simply want to subtract 6 hours which will be wrong half the year.
Anyone have any ideas? This seems to be a very common problem but I've been searching for a while, and have found nothing.
Using moment.js with the moment-timezone add-on makes this task simple.
// construct a moment object with UTC-based input
var m = moment.utc('2015-01-01 00:00:00');
// convert using the TZDB identifier for US Central time
m.tz('America/Chicago');
// format output however you desire
var s = m.format("YYYY-MM-DD HH:mm:ss");
Additionally, since you are referring to the entire North American Central time zone, you should say either "Central Time", or "CT". The abbreviation "CST" as applied to North America explicitly means UTC-6, while the "CDT" abbreviation would be used for UTC-5 during daylight saving time.
Do be careful with abbreviations though. "CST" might mean "China Standard Time". (It actually has five different interpretations).
You can use the time zone offset to determine whether 5 or 6 hours should be subtracted.
var dateJan;
var dateJul;
var timezoneOffset;
var divUTC;
var divCST;
// Set initial date value
dateValue = new Date('10/31/2015 7:29:54 PM');
divUTC = document.getElementById('UTC_Time');
divCST = document.getElementById('CST_Time');
divUTC.innerHTML = 'from UTC = ' + dateValue.toString();
// Get dates for January and July
dateJan = new Date(dateValue.getFullYear(), 0, 1);
dateJul = new Date(dateValue.getFullYear(), 6, 1);
// Get timezone offset
timezoneOffset = Math.max(dateJan.getTimezoneOffset(), dateJul.getTimezoneOffset());
// Check if daylight savings
if (dateValue.getTimezoneOffset() < timezoneOffset) {
// Adjust date by 5 hours
dateValue = new Date(dateValue.getTime() - ((1 * 60 * 60 * 1000) * 5));
}
else {
// Adjust date by 6 hours
dateValue = new Date(dateValue.getTime() - ((1 * 60 * 60 * 1000) * 6));
}
divCST.innerHTML = 'to CST = ' + dateValue.toString();
<div id="UTC_Time"></div>
<br/>
<div id="CST_Time"></div>
Maybe you can use something like the following. Note, that is just an example you might need to adjust it to your needs.
let cstTime = new Date(createdAt).toLocaleString("es-MX", {
timeZone: "America/Mexico_City" });
You can use below code snippet for converting.
function convertUTCtoCDT() {
var timelagging = 6; // 5 or 6
var utc = new Date();
var cdt = new Date(utc.getTime()-((1 * 60 * 60 * 1000) * timelagging));
console.log("CDT: "+cdt);
}
let newDate = moment(new Date()).utc().format("YYYY-MM-DD HH:mm:ss").toString()
var m = moment.utc(newDate);
m.tz('America/Chicago');
var cstDate = m.format("YYYY-MM-DD HH:mm:ss");
You can use below code snippet
// Get time zone offset for CDT or CST
const getCdtCstOffset = () => {
const getNthSunday = (date, nth) => {
date.setDate((7*(nth-1))+(8-date.getDay()));
return date;
}
const isCdtTimezoneOffset = (today) => {
console.log('Today : ', today);
let dt = new Date();
var mar = new Date(dt.getFullYear(), 2, 1);
mar = getNthSunday(mar, 2);
console.log('CDT Start : ', mar);
var nov = new Date(dt.getFullYear(), 10, 1, 23, 59, 59);
nov = getNthSunday(nov, 1);
console.log('CDT End : ', nov);
return mar.getTime()< today.getTime() && nov.getTime()> today.getTime();
}
var today = new Date()// current date
if (isCdtTimezoneOffset(today)) {
return -5
} else {
return -6
}
}
let cstOrCdt = new Date();
cstOrCdt.setHours(cstOrCdt.getHours()+getCdtCstOffset())
console.log('CstOrCdt : ', cstOrCdt);

Add future time to date and compare

I apologize if this question has been asked already but I couldn't find it for my problem.
I have seen this but am not sure what the number it returns represents: Date() * 1 * 10 * 1000
I'd like to set a future moment in time, and then compare it to the current instance of Date() to see which is greater. It could be a few seconds, a few minutes, a few hours or a few days in the future.
Here is the code that I have:
var futureMoment = new Date() * 1 *10 * 1000;
console.log('futureMoment = ' + futureMoment);
var currentMoment = new Date();
console.log('currentMoment = ' + currentMoment);
if ( currentMoment < futureMoment) {
console.log('currentMoment is less than futureMoment. item IS NOT expired yet');
}
else {
console.log('currentMoment is MORE than futureMoment. item IS expired');
}
Javascript date is based on the number of milliseconds since the Epoch (1 Jan 1970 00:00:00 UTC).
Therefore, to calculate a future date you add milliseconds.
var d = new Date();
var msecSinceEpoch = d.getTime(); // date now
var day = 24 * 60 * 60 * 1000; // 24hr * 60min * 60sec * 1000msec
var futureDate = new Date(msecSinceEpoc + day);
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
var futureMoment = new Date() * 1 *10 * 1000;
becomes
var now = new Date();
var futureMoment = new Date(now.getTime() + 1 *10 * 1000);
I think you mean to add time. Not multiply it.
If you deal with time, there is a lot of tools to choose.
Try moment library.
Used following code to compare selected date time with current date time
var dt = "Thu Feb 04 2016 13:20:02 GMT+0530 (India Standard Time)"; //this date format will receive from input type "date"..
function compareIsPastDate(dt) {
var currDtObj = new Date();
var currentTime = currDtObj.getTime();
var enterDtObj = new Date(dt);
var enteredTime = enterDtObj.getTime();
return (currentTime > enteredTime);
}

OADate to Milliseconds timestamp in Javascript

I try to do the exact opposite of :
What is equivalent of DateTime.ToOADate() in javascript?
Create a millisecond date (number of milliseconds since 1/1/1970) from a OADate (number of days since 30 dec 1899 as a double value)
my guess is I can do like this :
this.unixTimeStampFromOADate = function( OADateFloat)
{
var oaDateFor1970 = ?? ; //what's the value for 1/1/1970 in OADate format ?
var delta = Math.floor(OADateFloat - oaDateFor1970);
return delta*(1000*60*60*24);
}
so if I'm right, I need the value for 1/1/1970 in OADate format.
if I'm wrong, please can you suggest another conversion method ?
Javascript dates use a time value that is milliseconds since 1970-01-01T00:00:00Z. The time value for the date 1899-12-30 is -2209197600000.
To get the days since then, get the milliseconds for midnight at the start of today, subtract it from the OA epoch, divide by the ms in one day and get the absolute value. Note that the time values are all UTC so daylight saving, leap years, etc. are accounted for.
var epoch = new Date(1899, 11, 30); // 1899-12-30T00:00:00
var now = new Date(); // 2013-03-22T<current time>
now.setHours(0,0,0,0) // 2013-03-22T00:00:00
var oaDate = Math.abs((epoch - now) / 8.64e7); // 41355 for 2013-03-22
You can test it against some dates here (note that those dates are in the confusing US m/d/yy format).
Edit
Sorry, got the sense backwards. Here are some functions to go both ways.
Also took some time to work out that where it says "OLE Automation date is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899" actually means on or after 1899-12-30 00:00:00 and that the "fractional component represents the time on that day divided by 24". In other words, while 1899-12-29 00:00:00 is -1, the value for `899-12-29 06:00:00 is -1.25, not -0.75.
Anyhow, these functions seem to work now, but please test thoroughly:
var toOADate = (function () {
var epoch = new Date(1899,11,30);
var msPerDay = 8.64e7;
return function(d) {
var v = -1 * (epoch - d)/msPerDay;
// Deal with dates prior to 1899-12-30 00:00:00
var dec = v - Math.floor(v);
if (v < 0 && dec) {
v = Math.floor(v) - dec;
}
return v;
}
}());
var fromOADate = (function() {
var epoch = new Date(1899,11,30);
var msPerDay = 8.64e7;
return function(n) {
// Deal with -ve values
var dec = n - Math.floor(n);
if (n < 0 && dec) {
n = Math.floor(n) - dec;
}
return new Date(n*msPerDay + +epoch);
}
}());
var now = new Date();
var oaNow = toOADate(now);
var now2 = fromOADate(oaNow);
alert('Today: ' + now + '\nOADate: ' + oaNow + '\noaNow to Date: ' + now2);
The specification for OADate is confusing, particularly the way negative numbers are handled.
Edit Feb 2019
Updated version of functions, use local date values.
/* Convert a Microsoft OADate to ECMAScript Date
** Treat all values as local.
** #param {string|number} oaDate - OADate value
** #returns {Date}
*/
function dateFromOADate (oaDate) {
// Treat integer part is whole days
var days = parseInt(oaDate);
// Treat decimal part as part of 24hr day, always +ve
var ms = Math.abs((oaDate - days) * 8.64e7);
// Add days and add ms
return new Date(1899, 11, 30 + days, 0, 0, 0, ms);
}
/* Convert an ECMAScript Date to a Microsoft OADate
** Treat all dates as local.
** #param {Date} date - Date to convert
** #returns {Date}
*/
function dateToOADate (date) {
var temp = new Date(date);
// Set temp to start of day and get whole days between dates,
var days = Math.round((temp.setHours(0,0,0,0) - new Date(1899, 11, 30)) / 8.64e7);
// Get decimal part of day, OADate always assumes 24 hours in day
var partDay = (Math.abs((date - temp) % 8.64e7) / 8.64e7).toFixed(10);
return days + partDay.substr(1);
}
var now = new Date();
var x = dateToOADate(now);
console.log('Now: ' + now.toString());
console.log('As an OADate: ' + x);
console.log('Back to date: ' + dateFromOADate(x).toString());
window.onload = function(){
var el = document.getElementById('in')
el.addEventListener('change', function() {
var oaDate = dateToOADate(new Date(new Date(el.value)));
document.getElementById('out').value = oaDate;
document.getElementById('out2').value = dateFromOADate(oaDate);
});
}
input {width: 25em}
<table>
<tr>
<td>Input date:<br>(DD MMM YYYY HH:mm)
<td><input id="in" value="29 Dec 1899 06:00">
<tr>
<td>OA Date:
<td><input id="out" readonly>
<tr>
<td>Back to standard date:
<td><input id="out2" readonly>
</table>

How to ignore user's time zone and force Date() use specific time zone

In an JS app, I receive timestamp (eq. 1270544790922) from server (Ajax).
Basing on that timestamp I create Date object using:
var _date = new Date();
_date.setTime(1270544790922);
Now, _date decoded timestamp in current user locale time zone. I don't want that.
I would like _date to convert this timestamp to current time in city of Helsinki in Europe (disregarding current time zone of the user).
How can I do that?
A Date object's underlying value is actually in UTC. To prove this, notice that if you type new Date(0) you'll see something like: Wed Dec 31 1969 16:00:00 GMT-0800 (PST). 0 is treated as 0 in GMT, but .toString() method shows the local time.
Big note, UTC stands for Universal time code. The current time right now in 2 different places is the same UTC, but the output can be formatted differently.
What we need here is some formatting
var _date = new Date(1270544790922);
// outputs > "Tue Apr 06 2010 02:06:30 GMT-0700 (PDT)", for me
_date.toLocaleString('fi-FI', { timeZone: 'Europe/Helsinki' });
// outputs > "6.4.2010 klo 12.06.30"
_date.toLocaleString('en-US', { timeZone: 'Europe/Helsinki' });
// outputs > "4/6/2010, 12:06:30 PM"
This works but.... you can't really use any of the other date methods for your purposes since they describe the user's timezone. What you want is a date object that's related to the Helsinki timezone. Your options at this point are to use some 3rd party library (I recommend this), or hack-up the date object so you can use most of it's methods.
Option 1 - a 3rd party like moment-timezone
moment(1270544790922).tz('Europe/Helsinki').format('YYYY-MM-DD HH:mm:ss')
// outputs > 2010-04-06 12:06:30
moment(1270544790922).tz('Europe/Helsinki').hour()
// outputs > 12
This looks a lot more elegant than what we're about to do next.
Option 2 - Hack up the date object
var currentHelsinkiHoursOffset = 2; // sometimes it is 3
var date = new Date(1270544790922);
var helsenkiOffset = currentHelsinkiHoursOffset*60*60000;
var userOffset = _date.getTimezoneOffset()*60000; // [min*60000 = ms]
var helsenkiTime = new Date(date.getTime()+ helsenkiOffset + userOffset);
// Outputs > Tue Apr 06 2010 12:06:30 GMT-0700 (PDT)
It still thinks it's GMT-0700 (PDT), but if you don't stare too hard you may be able to mistake that for a date object that's useful for your purposes.
I conveniently skipped a part. You need to be able to define currentHelsinkiOffset. If you can use date.getTimezoneOffset() on the server side, or just use some if statements to describe when the time zone changes will occur, that should solve your problem.
Conclusion - I think especially for this purpose you should use a date library like moment-timezone.
To account for milliseconds and the user's time zone, use the following:
var _userOffset = _date.getTimezoneOffset()*60*1000; // user's offset time
var _centralOffset = 6*60*60*1000; // 6 for central time - use whatever you need
_date = new Date(_date.getTime() - _userOffset + _centralOffset); // redefine variable
Just another approach
function parseTimestamp(timestampStr) {
return new Date(new Date(timestampStr).getTime() + (new Date(timestampStr).getTimezoneOffset() * 60 * 1000));
};
//Sun Jan 01 2017 12:00:00
var timestamp = 1483272000000;
date = parseTimestamp(timestamp);
document.write(date);
Cheers!
I have a suspicion, that the Answer doesn't give the correct result. In the question the asker wants to convert timestamp from server to current time in Hellsinki disregarding current time zone of the user.
It's the fact that the user's timezone can be what ever so we cannot trust to it.
If eg. timestamp is 1270544790922 and we have a function:
var _date = new Date();
_date.setTime(1270544790922);
var _helsenkiOffset = 2*60*60;//maybe 3
var _userOffset = _date.getTimezoneOffset()*60*60;
var _helsenkiTime = new Date(_date.getTime()+_helsenkiOffset+_userOffset);
When a New Yorker visits the page, alert(_helsenkiTime) prints:
Tue Apr 06 2010 05:21:02 GMT-0400 (EDT)
And when a Finlander visits the page, alert(_helsenkiTime) prints:
Tue Apr 06 2010 11:55:50 GMT+0300 (EEST)
So the function is correct only if the page visitor has the target timezone (Europe/Helsinki) in his computer, but fails in nearly every other part of the world. And because the server timestamp is usually UNIX timestamp, which is by definition in UTC, the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT), we cannot determine DST or non-DST from timestamp.
So the solution is to DISREGARD the current time zone of the user and implement some way to calculate UTC offset whether the date is in DST or not. Javascript has not native method to determine DST transition history of other timezone than the current timezone of user. We can achieve this most simply using server side script, because we have easy access to server's timezone database with the whole transition history of all timezones.
But if you have no access to the server's (or any other server's) timezone database AND the timestamp is in UTC, you can get the similar functionality by hard coding the DST rules in Javascript.
To cover dates in years 1998 - 2099 in Europe/Helsinki you can use the following function (jsfiddled):
function timestampToHellsinki(server_timestamp) {
function pad(num) {
num = num.toString();
if (num.length == 1) return "0" + num;
return num;
}
var _date = new Date();
_date.setTime(server_timestamp);
var _year = _date.getUTCFullYear();
// Return false, if DST rules have been different than nowadays:
if (_year<=1998 && _year>2099) return false;
// Calculate DST start day, it is the last sunday of March
var start_day = (31 - ((((5 * _year) / 4) + 4) % 7));
var SUMMER_start = new Date(Date.UTC(_year, 2, start_day, 1, 0, 0));
// Calculate DST end day, it is the last sunday of October
var end_day = (31 - ((((5 * _year) / 4) + 1) % 7))
var SUMMER_end = new Date(Date.UTC(_year, 9, end_day, 1, 0, 0));
// Check if the time is between SUMMER_start and SUMMER_end
// If the time is in summer, the offset is 2 hours
// else offset is 3 hours
var hellsinkiOffset = 2 * 60 * 60 * 1000;
if (_date > SUMMER_start && _date < SUMMER_end) hellsinkiOffset =
3 * 60 * 60 * 1000;
// Add server timestamp to midnight January 1, 1970
// Add Hellsinki offset to that
_date.setTime(server_timestamp + hellsinkiOffset);
var hellsinkiTime = pad(_date.getUTCDate()) + "." +
pad(_date.getUTCMonth()) + "." + _date.getUTCFullYear() +
" " + pad(_date.getUTCHours()) + ":" +
pad(_date.getUTCMinutes()) + ":" + pad(_date.getUTCSeconds());
return hellsinkiTime;
}
Examples of usage:
var server_timestamp = 1270544790922;
document.getElementById("time").innerHTML = "The timestamp " +
server_timestamp + " is in Hellsinki " +
timestampToHellsinki(server_timestamp);
server_timestamp = 1349841923 * 1000;
document.getElementById("time").innerHTML += "<br><br>The timestamp " +
server_timestamp + " is in Hellsinki " + timestampToHellsinki(server_timestamp);
var now = new Date();
server_timestamp = now.getTime();
document.getElementById("time").innerHTML += "<br><br>The timestamp is now " +
server_timestamp + " and the current local time in Hellsinki is " +
timestampToHellsinki(server_timestamp);​
And this print the following regardless of user timezone:
The timestamp 1270544790922 is in Hellsinki 06.03.2010 12:06:30
The timestamp 1349841923000 is in Hellsinki 10.09.2012 07:05:23
The timestamp is now 1349853751034 and the current local time in Hellsinki is 10.09.2012 10:22:31
Of course if you can return timestamp in a form that the offset (DST or non-DST one) is already added to timestamp on server, you don't have to calculate it clientside and you can simplify the function a lot. BUT remember to NOT use timezoneOffset(), because then you have to deal with user timezone and this is not the wanted behaviour.
Presuming you get the timestamp in Helsinki time, I would create a date object set to midnight January 1 1970 UTC (for disregarding the local timezone settings of the browser).
Then just add the needed number of milliseconds to it.
var _date = new Date( Date.UTC(1970, 0, 1, 0, 0, 0, 0) );
_date.setUTCMilliseconds(1270544790922);
alert(_date); //date shown shifted corresponding to local time settings
alert(_date.getUTCFullYear()); //the UTC year value
alert(_date.getUTCMonth()); //the UTC month value
alert(_date.getUTCDate()); //the UTC day of month value
alert(_date.getUTCHours()); //the UTC hour value
alert(_date.getUTCMinutes()); //the UTC minutes value
Watch out later, to always ask UTC values from the date object. This way users will see the same date values regardless of local settings.
Otherwise date values will be shifted corresponding to local time settings.
My solutions is to determine timezone adjustment the browser applies, and reverse it:
var timestamp = 1600913205; //retrieved from unix, that is why it is in seconds
//uncomment below line if you want to apply Pacific timezone
//timestamp += -25200;
//determine the timezone offset the browser applies to Date()
var offset = (new Date()).getTimezoneOffset() * 60;
//re-initialize the Date function to reverse the timezone adjustment
var date = new Date((timestamp + offset) * 1000);
//here continue using date functions.
This point the date will be timezone free and always UTC, You can apply your own offset to timestamp to produce any timezone.
Use this and always use UTC functions afterwards e.g. mydate.getUTCHours();
function getDateUTC(str) {
function getUTCDate(myDateStr){
if(myDateStr.length <= 10){
//const date = new Date(myDateStr); //is already assuming UTC, smart - but for browser compatibility we will add time string none the less
const date = new Date(myDateStr.trim() + 'T00:00:00Z');
return date;
}else{
throw "only date strings, not date time";
}
}
function getUTCDatetime(myDateStr){
if(myDateStr.length <= 10){
throw "only date TIME strings, not date only";
}else{
return new Date(myDateStr.trim() +'Z'); //this assumes no time zone is part of the date string. Z indicates UTC time zone
}
}
let rv = '';
if(str && str.length){
if(str.length <= 10){
rv = getUTCDate(str);
}else if(str.length > 10){
rv = getUTCDatetime(str);
}
}else{
rv = '';
}
return rv;
}
console.info(getDateUTC('2020-02-02').toUTCString());
var mydateee2 = getDateUTC('2020-02-02 02:02:02');
console.info(mydateee2.toUTCString());
// you are free to use all UTC functions on date e.g.
console.info(mydateee2.getUTCHours())
console.info('all is good now if you use UTC functions')

Categories

Resources