toLocaleString()使用

  const num = 1000000;


  //整数部分每三位追加一个逗号
  num.toLocaleString(); //1,000,000


  //转换为百分比
  num.toLocaleString("zh", { style: "percent" }); // 100000000%


  //转换为货币显示
  num.toLocaleString("zh", { style: "currency", currency: "CNY" }); //¥1,000,000.00
  num.toLocaleString("zh", {
    style: "currency",
    currency: "cny",
    currencyDisplay: "code",
  }); //CNY 1,000,000.00
  num.toLocaleString("zh", {
    style: "currency",
    currency: "cny",
    currencyDisplay: "name",
  }); //1,000,000.00人民币


  const count = 45.6;
  //控制整数部分的最少位数和小数部分的最少和最多位数,如果不想有分隔符,可以指定useGrouping为false
  count.toLocaleString("zh", { minimumIntegerDigits: 5 }); //00,045.6
  count.toLocaleString("zh", { minimumIntegerDigits: 5, useGrouping: false }); //00045.6
  count.toLocaleString("zh", { minimumFractionDigits: 6 }); //45.600000
  count.toLocaleString("zh", { maximumFractionDigits: 0 }); //46


  //控制有效数字最少和最多位数
  count.toLocaleString("zh", { minimumSignificantDigits: 5 }); //45.600
  count.toLocaleString("zh", { maximumSignificantDigits: 2 }); //46


  const date = new Date();
  //格式化日期,12小时制
  date.toLocaleString("zh", { hour12: true }); //2022/2/7 下午6:03:05


  //格式化日期,24小时制
  date.toLocaleString("zh", { hour12: false }); //2022/2/7 18:04:12
  date.toLocaleString("zh", { timeZoneName: "short" }); //2022/2/7 GMT+8 下午6:07:32
  date.toLocaleString("zh", { timeZoneName: "long" }); //2022/2/7 中国标准时间 下午6:08:14
  
  
  //是否仅用两位数字表示
  date.toLocaleString("zh", {
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
  }); //2022/2/7 下午6:11:13
  date.toLocaleString("zh", {
    year: "2-digit",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
  }); //22/02/07 下午06:12:05
  
  
  //设置语言对月份的不同展现
  date.toLocaleString('en',{ month:'narrow' }) // F
  date.toLocaleString('en',{ month:'short' }) //Feb
  date.toLocaleString('en',{ month:'long' }) //February
posted @ 2023-11-30 14:42  天宁哦  阅读(186)  评论(0编辑  收藏  举报