通过utc-5检测日期并获取变量值

uklbhaso  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(354)

大家好,javascript中有这个变量

gateco = {
  "thursday": [{
    "open": "12:00",
    "close": "18:49"
  }],
  "friday": [{
    "open": "12:00",
    "close": "18:59"
  }]
};

如何获取utc-5时间的日期,并根据日期分别访问打开和关闭值?

a8jjtwal

a8jjtwal1#

你可以这样做。。。
请参见此处的时区列表

const
  dateTimeOption =
    { timeZone : 'Etc/GMT+5'
    , hour12   : false
    , weekday  : 'long'
    , hour     : '2-digit'
    , minute   : '2-digit' 
    }
, gateco =
    { thursday : [ { open: '12:00', close: '18:49' } ]  // why is it an Array ?
    , friday   : [ { open: '12:00', close: '18:59' } ] 
    } 

let isOpenNow  = false
  , [wDay,h,m] = new Intl.DateTimeFormat('en-US', dateTimeOption)
                  .format(new Date())
                  .split(/:| /)
                  .map(s=>Number(s)||s.toLowerCase() )                  

if ( gateco[wDay] )
  {
  let hm = (h *100) +m // simplied for comparing 
    , h_m_Open  = gateco[wDay][0].open
                    .split(':')
                    .reduce((hm,t,i)=>hm + (!i?100:1)*Number(t) ,0)
    , h_m_Close = gateco[wDay][0].close
                    .split(':')
                    .reduce((hm,t,i)=>hm + (!i?100:1)*Number(t) ,0)

  isOpenNow = (h_m_Open <= hm  && hm <= h_m_Close)
  }             
//console.log( wDay,h,m )
console.log( 'isOpenNow = ', isOpenNow)

相关问题