-1

I have the requirement to set particular time of the day to Date Object. The Time is in String and is CET, so "16:00" means "15:00" in UTC in Winter time. The following code does the job in node.js on my local machine which is in CET Timezone:

addTimetoDate(new Date(),"16:00");

function addTimetoDate(theDate,theTime){
    var dtDate = new Date(theDate)
   try{
    var strTime = theTime.replace(/ /g,'');
    var hourArray = strTime.split(":");
    dtDate.setHours(parseInt(hourArray[0]), parseInt(hourArray[1]), 0)
    
    if (dtDate == "Invalid Date"){
        dtDate = theDate;
    }

   } catch (e){
        dtDate = theDate;
    }
    return dtDate
}

However when deployed to remote server it produces Date Object which is offset by one hour the other direction when displayed with toLocaleString it shows "17:00". How to do it elegant way (as simple deduction of one hour will work only in Winter Time. ---EDIT--- To clarify the question is - Is there anything I can do prior to using .setHours to make it right. Or I should not use setHours but rather manipulate the string for Date Constructor, so 16.00 CET gets properly converted to UTC representation?

MatnikR
  • 181
  • 2
  • 16
  • What is the question here? [Is the Javascript date object always one day off?](https://stackoverflow.com/q/7556591/215552) (one hour off in your case); [How to add hours to a Date object?](https://stackoverflow.com/q/1050720/215552); Maybe [Javascript Date Object from string in form 'HH-MM'](https://stackoverflow.com/q/4332906/215552)? [Getting the client's time zone (and offset) in JavaScript](https://stackoverflow.com/q/1091372/215552) – Heretic Monkey Nov 15 '22 at 22:28
  • Edited the question to address your concern – MatnikR Nov 16 '22 at 08:52
  • The use of *parseInt* is entirely unnecessary, as is *try..catch*, since the Date *set* methods never throw errors. If the provided value is not suitable they just return NaN, so the *catch* block will never be executed. – RobG Nov 17 '22 at 08:37
  • You are right - that is obsolete, on the positive side it should work. I am surprised that there is no simpler way to do it. In lots of applications there is a scenario where users separately plays with "Date only" and "Time only" fields where Time field is in users local timezone. I answered this myself as nobody suggested anything better. – MatnikR Nov 18 '22 at 21:38

2 Answers2

0

toLocaleString will convert the given time into the timezone defined by the locale,... and that's fine if the time is UTC to start with. But if it's already been offset, then it's going to offset it again, which is what you're seeing.

Time is a fiddly creature; when I'm working with time I always store it (eg in a database) as UTC and let the client descide how it gets displayed. That way I can guarantee no server-side silliness.

Noscere
  • 417
  • 3
  • 8
-1

Ok Found the solution:

function addTimetoDate(theDate,theTime){
var d = new Date();
var hourLocal = d.toLocaleString({timezone:'Europe/Berlin'}).split(" ")[1].split(":")[0]
var hourISO = d.toISOString().split("T")[1].split(":")[0]
var diff = parseInt(hourLocal) - parseInt(hourISO);
var dtDate = new Date(theDate)
try{
var strTime = theTime.replace(/ /g,'');
var hourArray = strTime.split(":");

dtDate.setHours(parseInt(hourArray[0])-diff, parseInt(hourArray[1]), 0)

if (dtDate == "Invalid Date"){
    dtDate = theDate;
}

} catch (e){
   dtDate = theDate;
}
return dtDate
}

Diff in whatever situation - being on the server, or on the browser captures current status (including daylight saving change) of hourly difference for the given Timezone. Then you can adjust your given hour to UTC to use setHours.

MatnikR
  • 181
  • 2
  • 16