我想將默認時間設定為比當前時間晚 4 小時,因此如果它是 2021 年 10 月 31 日凌晨 3:00,則默認時間應顯示為 2021 年 10 月 31 日凌晨 4:00,并且時間為 2021 年 10 月 31 日 10: 00 pm 它應該顯示 2021 年 11 月 1 日凌晨 2:00
我嘗試將當前時間加到 4,但它打破了夜間時間。
const currentDate = new Date();
const dateTime = `${currentDate.getFullYear()}-${currentDate.getMonth() 1}-${currentDate.getDate()}T${
currentDate.getHours() 3
}:${currentDate.getMinutes()}`;
<TextField
id="datetime-local"
type="datetime-local"
defaultValue={`${dateTime}`}
InputLabelProps={{
shrink: true,
}}
InputProps={{ inputProps: { min: `${dateTime}` } }}
onChange={handleChange}
/>
</div>
uj5u.com熱心網友回復:
在這種情況下,您應該添加之前的小時數,即使小時數高于 24 小時,javascript Date 也會處理并回傳正確的小時數。
const currentDate = new Date();
currentDate.setHours(currentDate.getHours() 4);
現在您可以使用 Date 物件。
uj5u.com熱心網友回復:
您可以使用Date 物件中的setHours和getHours方法。
const currentDate = new Date();
const fourHoursLater = new Date(
currentDate.setHours(currentDate.getHours() 4)
);
代碼沙盒
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344873.html
