zoukankan      html  css  js  c++  java
  • [Javascript] Natively Format JavaScript Dates and Times

    The ability to natively format dates and times using the browser is impressive. You may find yourself not needed to grab libraries such as moment.js or date-fns when doing simple formatting and better yet, the browser support is surprisingly good!

    const date = new Date();
    
    console.log(date.toLocaleDateString());
    console.log(date.toLocaleDateString("en-US"));
    console.log(date.toLocaleDateString("fr-FR"));
    const dateOptions = {
      weekday: "long",
      year: "numeric",
      month: "long",
      day: "numeric"
    };
    console.log(date.toLocaleDateString("en-US", dateOptions));
    console.log(
      date.toLocaleDateString("en-US", {
        month: "short",
        day: "numeric"
      })
    );
    console.log(
      date.toLocaleDateString("fr-FR", {
        month: "long"
      })
    );
    console.log(date.toLocaleTimeString());
    console.log(date.toLocaleTimeString("en-US"));
    console.log(date.toLocaleTimeString("de-DE"));
    const timeOptions = {
      hour12: true,
      hour: "numeric",
      minute: "2-digit",
      second: "2-digit",
      timeZone: "America/Los_Angeles"
    };
    console.log(date.toLocaleTimeString("en-US", timeOptions));
    console.log(
      date.toLocaleTimeString("en-US", {
        hour: "numeric",
        minute: "2-digit"
      })
    );
    console.log(
      date.toLocaleTimeString("en-US", {
        hour12: false
      })
    );
    console.log(
      date.toLocaleString("en-US", {
        ...dateOptions,
        ...timeOptions
      })
    );
    const dateTimeFormat = new Intl.DateTimeFormat("en-US", {
      ...dateOptions,
      ...timeOptions
    });
    console.log(dateTimeFormat.format(date));
    const anotherDate = new Date("2000-12-25T12:34:56.789Z");
    console.log(dateTimeFormat.format(anotherDate));
  • 相关阅读:
    【09-04】java内部类学习笔记
    【09-03】java泛型学习笔记
    【08-23】redis学习笔记
    【06-23】js动画学习笔记01
    【11-23】mysql学习笔记02
    【06-18】CentOS使用笔记
    thinkphp+datatables+ajax 大量数据服务器端查询
    python遗传算法实现数据拟合
    简单的新闻客户端APP开发(DCloud+thinkphp+scrapy)
    python游戏编程——跟13岁儿童学编程
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13443979.html
Copyright © 2011-2022 走看看