在 javascript 中转换格式不正确的日期

Convert poorly formatted date in javascript

我想在我的地图上隐藏已经出现的开放日房源。我仅有的日期信息如下所示:

Public: Sat Sep 18, 10:00AM-2:00PM

我需要能够将此日期与今天进行比较,以便我可以创建一个 if 语句来隐藏过期的开放日。

使用这段代码我能够截断开头的文本:

const str = 'Public: Thu Feb 11, 10:00AM-2:00PM';
const opdate = (str.substr(8));
console.log(opdate); // Displays "Thu Feb 11, 10:00AM-2:00PM"

这就是我现在卡住的地方...我不知道如何将其转换为另一种格式以便能够将其与今天的日期进行比较。它错过了我确定会在每年年底引起问题的年份,并且它有一个结束时间,这实际上不是必需的,因为我只需要将实际日期与今天的日期进行比较。

类似的东西?

let {open,close} = get_OpenCloseDates('Public: Thu Feb 11, 10:00AM-2:00PM')

console.log( 'open ->', open.toLocaleString() )
console.log( 'close ->', close.toLocaleString() )

function get_OpenCloseDates( str )
  {
  let year   = new Date().getFullYear()
  , [wD,mth,d,h1,m1,h2,m2] = str.substr(8).split(/[\s,-]+|:/)
    ;
  return ({open: cDate(mth,d,h1,m1,year), close: cDate(mth,d,h2,m2,year)})

  function cDate(mth,d,h,m,y) 
    {    
    let mN = new Date(`${mth} 1, ${y}`).getMonth() 
    return new Date( y, mN, +d, +h +(m.includes('PM')?12:0),parseInt(m),0)
    }
  }

您可以使用与您拥有的时间戳相同的格式为日期创建时间戳,并查看它是否在字符串中,例如

// Return date in format DDD MMM D, e.g. Thu Feb 11
function openHouseFormat(date = new Date()) {
  let {month, weekday, day} = new Intl.DateTimeFormat('en',{
    weekday:'short',
    month: 'short',
    day: 'numeric'
  }).formatToParts(date).reduce((obj, part) => {
    obj[part.type] = part.value;
    return obj;
  }, {});
  return `${weekday} ${month} ${day}`;
}

// Check if date is in string, e.g.
// is Thu Feb 11 in Public: Thu Feb 11, 10:00AM-2:00PM
function isSameDay(openHouseDate, date = new Date()) {
  return new RegExp(openHouseFormat(date)).test(openHouseDate);
}

// Date to test
let openHouseDate = 'Public: Thu Feb 11, 10:00AM-2:00PM';
console.log(isSameDay(openHouseDate, new Date(2021,1,11))); // true
// Test today, i.e. not 2021-02-11
console.log(isSameDay(openHouseDate, new Date())); // false

您可以使用 indexOf 而不是正则表达式,这可能会更有效率。

鉴于“开放日”日期是 Date.prototype.toString 的子字符串,您可以将事情简化为:

function isSameDate(publicString, date = new Date()) {
  return publicString.indexOf(date.toString().slice(0,10)) != -1;
}

// Example
let publicString = 'Public: Sat Sep 18, 10:00AM-2:00PM';
[new Date(2021,8,18), // 18 Sep
 new Date()           // today
].forEach(d => console.log(isSameDate(publicString, d)));

年份真的没有必要,因为“Sat Sep 18”大约每 7 年才会出现一次(上一次是 2014 年,下一次是 2027 年)。

好的,这是我根据@Mister Jojo 的第一个建议提出的解决方案。这需要我格式不正确的日期,添加当前年份,然后与今天进行比较,以便我可以执行 if else 规则。感谢大家的帮助。我喜欢这个网站。

const str = 'Public: Tue Dec 21, 10:00AM-2:00PM';
const [weekday,month,day,h1,h2] = str.substr(8).split(/[\s,-]+/);
var d = new Date();
var n = d.getFullYear();
var openhousedate = [month,day, n].join(" ");
var formatted = openhousedate;
//results in MMM DD YYYY



const javaScriptRelease = Date.parse(formatted);

const javaScriptRelease2 = Date.parse("2021/09/20");
document.write(javaScriptRelease + "<br>");
document.write(javaScriptRelease2 + "<br>");


console.log(javaScriptRelease2);


if (javaScriptRelease2 > javaScriptRelease) {
    document.write(' its in the past ');
} else {
    document.write(' open house is coming up ');
}