簡體   English   中英

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

[英]How do you reverse String Interpolation in Javascript

好的,可以說我有一個每天都會更改的字符串,其中包含日期

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

可能有比這更優雅的方法,但這是我想到的第一件事。 拆分字符串,並使用它創建一個日期對象。

 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); 

與使用實際的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); } 

如果您可以使用第三方庫,那么此刻將使操作變得異常簡單:

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,日等)
  • MMMM整月文字
  • YYYY 4位數字的年份

輸出格式遵循相同的規則,可在此處找到。 如果您打算進行更多的時間計算,那么與這里的其他答案相比,我只會推薦這種方法。 否則,導入庫可能會過大!

您可以查找日期模式,獲取值,然后使用它們創建日期。 最好使解析盡可能不依賴整個字符串,以便可以在一定程度上容忍更改。

下面只是在字符串末尾查找日期部分(例如,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*) ([az]+) (\\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,則存在一些錯誤處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM