我正試圖在我的地圖上隱藏已經發生的開放房屋串列。我所擁有的唯一的日期資訊是這樣的:
Public: Sat Sep 18, 10: 00AM-2: 00PM
我需要能夠將這個日期與今天進行比較,這樣我就可以創建一個if陳述句來隱藏過期的開放房屋。
使用這段代碼,我能夠切斷開頭的文本:
我需要將這個日期與今天進行比較,以便我能夠創建一個if陳述句來隱藏過期的開放房屋。
const str = '公眾。2月11日星期四,上午10:00-下午2:00'。
const opdate = (str.substr(8) )。)
console.log(opdate); //顯示 "Thu Feb 11, 10:00AM-2:00PM"
這就是我現在被卡住的地方......我不知道如何將其轉換為其他格式,以便能夠將其與今天的日期進行比較。 它缺少年份,我確信這將在每年的年底造成問題,而且它有一個結束時間,這真的沒有必要,因為我只需要將實際日期與今天的日期進行比較。
uj5u.com熱心網友回復:
類似的東西?
。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'/span>)? 12:0),parseInt(m),0)
}
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以為日期創建一個與你所擁有的時間戳相同格式的時間戳,看看它是否在字串中,例如:
。// return date in format DDD MMM D, 例如 Thu Feb 11
function openHouseFormat(date = new Date()) {
let {month, weekday, day} = new Intl.DateTimeFormat('en'/span>,{
weekday:'short'。
月。'short',
日: 'numeric'.
}).formatToParts(date).reduce((obj, part) =>/span> {
obj[part.type] = part.value。
return obj;
}, {});
return `${weekday} ${month} ${day}`。
}
//檢查日期是否在字串中,例如:。
//是Thu Feb 11 in Public。2月11日星期四,上午10:00-下午2:00
function isSameDay(openHouseDate, date = new Date()) {
return new RegExp(openHouseFormat(date))。 test(openHouseDate)。
}
// Date to test。
let openHouseDate = '公眾。Thu Feb 11, 10:00AM-2:00PM'。
console. log(isSameDay(openHouseDate, new Date(2021,1,11) )。// true>
//測驗今天,即不是2021-02-11。
console。 log(isSameDay(openHouseDate, new Date()) )。// false
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
你可以使用indexOf代替正則運算式,這可能會更有效一點。
。function isSameDate(publicString, date = new Date()) {
return publicString.indexOf(date.toString)。 slice(0,10) != -1;
}
// Example
let publicString = 'Public: 9月18日星期六,10:00AM-2:00PM'。
[new Date(2021, 8,18), //18 Sep.
new Date() /今天
].forEach(span class="hljs-params">d => console. log(isSameDate(publicString, d));。
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
年份真的沒有必要,因為 "9月18日星期六 "大約每7年才會出現一次(上一次是2014年,下一次是在2027年)。
uj5u.com熱心網友回復:
好吧,這是我根據@Mister Jojo的第一個建議想出的解決方案。 這是將我糟糕的格式化的日期,加上當前的年份,然后與今天進行比較,這樣我就可以做一個if else規則。 謝謝大家的幫助。 我愛這個網站。
const str = '公眾。12月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(" "/span>)。
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 ')。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328392.html
標籤:
