【问题标题】:Date to localeString with milliseconds?以毫秒为单位的日期 tolocaleString?
【发布时间】:2019-05-04 23:45:50
【问题描述】:

在 Javascript 中,我尝试使用 toLocaleString() 函数将 Date 对象转换为语言环境字符串。我想要的是以毫秒为单位的转换后的语言环境字符串。这可能吗?

const time = "2018-12-03T16:24:05.150Z";
const date = new Date(time);
const str = date.toLocaleString();

console.log(date.toLocaleString()); //3-12-2018 17:24:05

【问题讨论】:

  • 请分享您已经尝试过的代码。
  • 将代码添加到问题中
  • 你需要使用像moment.js这样的库
  • @theapologist 我想要一个以毫秒为单位的语言环境字符串。不将日期转换为毫秒。
  • 我不认为你可以用toLocaleString() 做到这一点,因为它没有原生的option,而且跨语言环境的输出也不够一致,无法简单地连接毫秒。例如,我的输出以AM 结束,而你的以一天中的时间结束。

标签: javascript date


【解决方案1】:

const time = "2018-12-03T16:24:05.150Z";
const date = new Date(time);
const str = date.toLocaleString();
const result = new Date(str).getTime();

console.log(result);

【讨论】:

    【解决方案2】:

    如前所述,缺少毫秒关键字。
    我为练习构建了一个 JavaScript 时钟并遇到了同样的问题,但我已经为 toLocaleString 构建了一个界面,所以我决定添加我自己的功能,它可能不适用于所有语言,但已经足够好了。
    我选择 毫秒 作为关键字。我认为作为格式信息,结合使用 n-digit,如 2-digitanything 用于没有特定的毫秒格式(因为还包括 numeric)就足够了。 当hourCycle 的格式定义为h23h24空格 时,我使用冒号 连接。

    (sn-p中的代码没有错误处理,触发器是onchange所以填写en然后将hourCycle改为h11

    function toLocalStringFromDate(nDate, nLanguage, nOptions){
        if("millisecond" in nOptions){ // own keyword option
            if(nOptions.millisecond.endsWith("-digit")){
                let tDigits = parseInt(nOptions.millisecond.substring(0, nOptions.millisecond.length-6));
                // extract amount of digits from format
                let tLength = (""+nDate.getMilliseconds()).length;
                // length of current milliseconds
                return nDate.toLocaleString(nLanguage, nOptions)
                    // basic formatting
                    +("hourCycle" in nOptions && (nOptions.hourCycle == "h23" || nOptions.hourCycle == "h24") ? ':' : ' ')
                    // separator
                    +("0".repeat(tDigits)+nDate.getMilliseconds()).substring(tLength, tDigits+tLength);
                    // n-digit of zeros followed by milliseconds shifted by n digit is substring(tDigits+tLength-tDigits, tDigits+tLength);
            }
            else{
                return nDate.toLocaleString(nLanguage, nOptions)+("hourCycle" in nOptions && (nOptions.hourCycle == "h23" || nOptions.hourCycle == "h24") ? ':' : ' ')+nDate.getMilliseconds();
            }
        }
        else{
            return nDate.toLocaleString(nLanguage, nOptions);
        }
    }
    
    window.document.body.children[1].lastElementChild.value = "{\"hourCycle\": \"h23\", \"millisecond\": \"3-digit\"}";
    input{width: 60%;}
    <p><b>Language: </b><input onchange="console.log(toLocalStringFromDate(new Date(), this.value, JSON.parse(this.parentElement.nextElementSibling.lastElementChild.value)));"></p>
    <p><b>Options: </b><input onchange="console.log(toLocalStringFromDate(new Date(), this.parentElement.previousElementSibling.lastElementChild.value, JSON.parse(this.value)));"></p>

    【讨论】:

      【解决方案3】:

      您可以使用以下代码行实现此目的

      const date = new Date();
      const localDateTime = date.toLocaleString();
      const currentDateObj = new Date(localDateTime); 
      const convertLocalDateTimeToMS = currentDateObj.getTime(); 
      console.log(convertLocalDateTimeToMS); // 1630060060000

      【讨论】:

      • currentDateObj 是无效日期。语言环境字符串不能总是被解析回 Date 对象。
      • 你能告诉我它在哪里显示为无效日期。
      • 当然,在法国运行您的前 3 行会使 currentDateObj 成为无效日期,从而使您的 sn-p 显示 NaNlocalDateTime 的值为“27/08/2021, 18:23:11”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多