如何在Javascript中反转字符串插值

时间:2018-07-15 04:52:29

标签: javascript date

好的,可以说我有一个每天都会更改的字符串,其中包含日期

var receiveddate = "Received on Saturday 14th of July 2018"

如何将日期提取到示例formatreceiveddate = "2018-07-14"

我知道另一种方法是使用字符串插值和模板文字,而不是如何反转

所以我真正要问的是如何改变这种情况

Received on Saturday 14th of July 2018
Received on Saturday 5th of May 2018
Received on Monday 8th of January 2018
Received on Wednesday 19th of July 2017
Received on Sunday 1st of July 2018
Received on Tuesday 3rd of July 2018
Received on Saturday 2nd of June 2018
Received on Thursday 21st of June 2018
Received on Thursday 31st of May 2018 

每个日期进入此2018-07-14

4 个答案:

答案 0 :(得分:3)

可能有比这更优雅的方法,但这是我想到的第一件事。拆分字符串,并使用它创建一个日期对象。

const dateString = "Received on Saturday 14th of July 2018";

// Split the string up by spaces
const dateParts = dateString.split(' ');

// Grab each part of the date. We parse the day as an int to just get the numeral value
const month = dateParts[5];
const day = parseInt(dateParts[3]);
const year = dateParts[6];

// Parse the date by reassembling the string
const date = new Date(month + ' ' + day + ' ' + year);

// Output in your desired format (ISO)
const formattedDate = date.getFullYear()+'-' + (date.getMonth()+1) + '-'+date.getDate();

console.log(formattedDate);

答案 1 :(得分:0)

与使用实际的Date对象相比,使用正则表达式的“字符串方法”可能很幼稚,但它很简单:

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var regex = /received on \w+ (\d+).*?of (\w+) (\w+)/ig;
    var result;
    while (result = regex.exec(input)) {
      var month = months.indexOf(result[2]) + 1;
      console.log(result[3] + "-" + month + "-" + result[1]);
    }

//with zero-padding
var input = `Received on Saturday 14th of July 2018
Received on Saturday 5th of May 2018
Received on Monday 8th of January 2018
Received on Wednesday 19th of July 2017
Received on Sunday 1st of July 2018
Received on Tuesday 3rd of July 2018
Received on Saturday 2nd of June 2018
Received on Thursday 21st of June 2018
Received on Thursday 31st of December 2018`

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
var regex = /received on \w+ (\d+).*?of (\w+) (\w+)/ig;
var result;
while (result = regex.exec(input)) {
  var month = months.indexOf(result[2]) + 1;
  month = /\d{2,}/.test(month) ? month : "0" + month;
  var day = result[1];
  day = /\d{2,}/.test(day) ? day : "0" + day;
  console.log(result[3] + "-" + month + "-" + day);
}

答案 2 :(得分:0)

如果您可以使用第三方库,moment可以使这一操作变得非常简单:

let dtStr = moment(
   "Received on Saturday 14th of July 2018",
   "[Received on] dddd Do [of] MMMM YYYY"
).format("YYYY-MM-DD");
// dtStr = "2018-07-14"

根据文档,moment构造函数将输入日期作为第一个参数,并将可选的格式字符串作为第二个参数。格式字符串的快速细分:

  • 方括号表示转义文本
  • dddd全天文本
  • Do每月的某天,带有后缀修饰符(st,th等)
  • MMMM整月文本
  • YYYY 4位数字年份

输出格式遵循相同的规则,可以在here中找到。如果您打算进行更多的时间计算,那么与这里的其他答案相比,我只会推荐这种方法。否则,导入库可能会过大!

答案 3 :(得分:-1)

您可以查找日期模式,获取值并使用它们创建日期。最好使解析尽可能不依赖整个字符串,以便可以在一定程度上容忍更改。

以下内容只是在字符串末尾查找日期部分(例如2018年7月14日),而第一部分是什么都没关系。它甚至可以容忍像“ 2018年7月14日”之类的日期的字符串。 例如

function parseString(s) {
  var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' '),
      parts  = s.match(/(\d+)(st|rd|th)?( ?\w*) ([a-z]+) (\d{4})$/i) || [],
      day    = parts[1],
      month  = (parts[4] || '').toLowerCase().slice(0,3),
      year   = parts[5];

  return new Date(year, months.indexOf(month), day);
}

[ 'Received on Saturday 14th of July 2018',
  'Received on Saturday 5th of May 2018',
  'Received on Monday 8th of January 2018',
  'anything you like, even War and Peace 19th July 2017',
  '31 December 2012',
  'Totally invalid string'
].forEach(s => {
  var d = parseString(s);
  console.log(isNaN(d)? d : d.toString());
});

如果 match 找不到匹配项并返回 null ,则存在一些错误处理。