NetTalk Central

Author Topic: JavaScript to Clarion Date/Time Functions  (Read 5661 times)

DonRidley

  • Don Ridley
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 729
  • donaldridley2011@gmail.com
    • View Profile
    • Email
JavaScript to Clarion Date/Time Functions
« on: June 27, 2019, 08:36:09 AM »
I thought I would share these with anyone who is interested.  Big thank you to Bruce for giving me a few clues.  Also, thanks to Steve Parker for sharing some of his date/time methods with me (converted to JavaScript).  I am no JS expert so feel free to modify or fix as needed.

function getClarionTime(clariontime) {
   var d = new Date(),
   clariontime = parseInt((d.getTime() - d.setHours(0,0,0,0)) / 10)
   return clariontime;
}

function getClarionDate(clariondate) {
   thisdate = new Date();
   day = thisdate.getDay();
   month = thisdate.getMonth()+1;
   year = thisdate.getYear();
   date = Date(year,month,day);
   clariondate = parseInt(((Date.parse(date) / 86400) / 1000) + 61730);
   return clariondate;
}


function createStarDate(date,time,stardate) {
   //needs to be Clarion Date/Time
   stardate = date + (time / 8640000)
   return stardate;
}

function createRbnFromDateTime(date,time,rbn) {
   //needs to be Clarion Date/Time
   rbn = parseInt(time + (date * 8640000))
   return rbn;
}

Don
« Last Edit: June 27, 2019, 08:41:44 AM by DonRidley »
"Eliminate the impossible, whatever remains, however unlikely, must be the truth."

NetTalk 12.55
Clarion 11

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: JavaScript to Clarion Date/Time Functions
« Reply #1 on: June 27, 2019, 11:41:44 PM »
Cool work Don.
This can be simplified down just a bit though, with the following;
I've also allowed for the return value to be in both UTC or Local time, whichever you need.

// returns date in Clarion Date format. Default is local time (of the browser), pass 1 in parameter to get utc time.
function clarionToday(utc){
  time = Date.now()/10;
  if (!utc){
    d = new Date();
    time -= d.getTimezoneOffset()*6000
  }
  return (parseInt(time / 8640000) + 61730)    
}

// returns time in Clarion time format. Default is local time (of the browser), pass 1 in parameter to get utc time.
function clarionClock(utc){
  time = Date.now()/10;
  if (!utc){
    d = new Date();
    time -= d.getTimezoneOffset()*6000
  } 
  return (parseInt(1+(time % 8640000)))
}

I've added these to the next build (in netweb.js). So you can cut & paste them into your netweb.js now if you want to use them.

cheers
Bruce

DonRidley

  • Don Ridley
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 729
  • donaldridley2011@gmail.com
    • View Profile
    • Email
Re: JavaScript to Clarion Date/Time Functions
« Reply #2 on: June 28, 2019, 01:48:15 AM »
Thank you for adding those (and cleaning them up a little).  Netweb.js is a pretty nice repository of JS tools.

Don
"Eliminate the impossible, whatever remains, however unlikely, must be the truth."

NetTalk 12.55
Clarion 11