【问题标题】:Get Milliseconds of specific date and time [duplicate]获取特定日期和时间的毫秒数[重复]
【发布时间】:2021-05-31 04:06:49
【问题描述】:

我想获取市场开盘时间的毫秒数(09:30 am cst)

  //date
  const date = new Date();
  const ddd = moment().format('ddd');
  const d = moment().format('D');
  const mmm = moment().format('MMM');
  const yyyy = moment().format('yyyy');

 

  const openMills = moment("2021-03-01 14:30:00GMT").format('x');

  console.log(openMills);

错误:

弃用警告:提供的值不是可识别的 RFC2822 或 ISO 格式。

【问题讨论】:

  • 您是否尝试使用警告要求您使用的格式之一,例如 ISO8601?
  • 尝试从日期中删除GMT2021-03-01 14:30:00?
  • 为什么只有8行代码会引起警告? -> minimal reproducible example

标签: javascript datetime momentjs utc


【解决方案1】:
export class DateTimeHelper {
  static getMills = (type) => {
    //date
    const date = new Date();
    const ddd = moment().format('ddd');
    const dd = moment().format('DD');
    const mm = moment().format('MM');
    const yyyy = moment().format('yyyy');
  
  
   const utcDate = new Date(yyyy +'-' +mm+'-'+dd+' 14:30:00');
   const utcDateClose = new Date(yyyy +'-' +mm+'-'+dd+' 21:00:00');
  
   const myOpenDate: any = new Date(Date.UTC(
      utcDate.getFullYear(),
      utcDate.getMonth(),
      utcDate.getDate(),
      utcDate.getHours(),
      utcDate.getMinutes()
   ));
  
   const myCloseDate: any = new Date(Date.UTC(
    utcDateClose.getFullYear(),
    utcDateClose.getMonth(),
    utcDateClose.getDate(),
    utcDateClose.getHours(),
    utcDateClose.getMinutes()
  ));
   
   const openTimeInMills = Date.parse(myOpenDate);
   const closeTimeInMills = Date.parse(myCloseDate);
  
   console.log(openTimeInMills);
   console.log(closeTimeInMills);

   if(type === 'open'){
    return openTimeInMills;
   }

   if(type === 'close'){
    return closeTimeInMills;
   }
 

  }
}

【讨论】:

  • 纯代码的答案没有那么有用。一个好的答案应该解释为什么 OP 有他们的问题以及您的答案如何解决它。
猜你喜欢
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多