代码之家  ›  专栏  ›  技术社区  ›  KitKit

从JavaScript中的YYYY-DD-MM字符串中提取日、月、年

  •  0
  • KitKit  · 技术社区  · 6 年前

    我正在寻找从字符串中提取日、月、年的最佳解决方案 YYYY-DD-MM 在JavaScript中:

    提取:

    2019-25-01

    目的:

    { day: 25, month: 01, year: 2019 }

    最好的方法是什么?提前感谢!

    7 回复  |  直到 6 年前
        1
  •  17
  •   Nina Scholz    6 年前

    您可以拆分、销毁并返回一个新对象。

    const getDate = string => (([year, day, month]) => ({ day, month, year }))(string.split('-'));
    
    console.log(getDate('2019-25-01'));
        2
  •  4
  •   CertainPerformance    6 年前

    我将使用正则表达式 match 每个数字序列,将匹配的字符串数组映射到数字,销毁为变量,然后从中创建一个对象:

    const [year, day, month] = '2019-25-01'
      .match(/\d+/g)
      .map(Number);
    const obj = { day, month, year };
    console.log(obj);

    注意数字不能有前导零。如果希望月份前导零,请使用字符串(只需删除 .map(Number) )

        3
  •  4
  •   adiga    6 年前

    这是一个非常短和快速的解决方案,只适用于这种格式和ES6

    function getJsonDate(text) {
      var {0: year, 1: day, 2: month } = text.split("-");
      return { day, month, year};
    }
    console.log(getJsonDate("2019-25-1"));

    如果您需要字段是数字,那么可以添加一个映射,如下所示:

    function toNumber(text) {
      text = text - 0;
      return isNaN(text) ? 0 : text;
    }
    function getJsonDate(text) {
      var {0: year, 1: day, 2: month } = text.split("-").map(toNumber);
      return { day, month, year};
    }
    console.log(getJsonDate("2019-25-1"));
        4
  •  1
  •   flyingfox    6 年前

    你可以 split() 做这件事

    var value = "2019-25-01";
    var year = value.substring(0,4);
    var day = value.substring(5,7);
    var month = value.substring(8,10);
    var str = "{day:" + day + ",month:" + month + ",year:" + year + "}";
    console.log(str);
        5
  •  1
  •   holydragon    6 年前

    使用 .split() .

    let date = "2019-25-01"
    let dateArr = date.split('-')
    let obj = {
      day: dateArr[1],
      month: dateArr[2],
      year: dateArr[0]
    }
    console.log(obj)
        6
  •  1
  •   Moak    6 年前

    对于类似JSON的结构

    d="2019-25-01";
    x=d.split("-");
    json="{ day: "+x[1]+", month: "+x[2]+", year: "+x[0]+" }";
    >>"{ day: 25, month: 01, year: 2019 }"
    
        7
  •  1
  •   Shidersz    6 年前

    这里有一种方法不需要进行映射 str -> array -> object ,它将转换 string 直接到 object 也可以用于更一般化的日期和时间。它是基于 replacement 可用于的函数 String::replace()

    const dateStr1 = "2019-25-01";
    const dateMap1 = ["year", "day", "month"];
    const dateStr2 = "2019-25-01 17:07:56";
    const dateMap2 = ["year", "day", "month", "hour", "minute", "second"];
    
    const splitDate = (str, map) =>
    {
        let obj = {}, i = 0;
        str.replace(/\d+/g, (match) => obj[[map[i++] || i - 1]] = match);
    
        return obj;
    }
    
    console.log(splitDate(dateStr1, dateMap1));
    console.log(splitDate(dateStr2, dateMap2));

    另一种与日期格式严格相关的方法可能是下一种:

    const strDate = "2019-25-01";
    
    const splitDate = (str) =>
    {
        let [date, year, day, month] = str.match(/(\d+)-(\d+)-(\d+)/);
        return {year, month, day};
    }
    
    console.log(splitDate(strDate));