使用 toLocaleString 将日期格式设置为 MM/DD/YYYY

在 JS 中,如何将日期格式化为 MM/DD/YYYY?


new Date(Date.now() + (8 * 86400000)).toLocaleString().split(',')[0])

returns "12/1/2020"

如何将其格式化为“12/01/2020”?


fromDate:

(new Date(Date.now() + (1 * 86400000)).toLocaleString().split(',')[0]),

toDate:

(newDate(Date.now() + (8 * 86400000)).toLocaleString().split(',')[0])

我希望 fromDate 和 toDate 为:


如果在 MST 下午 5:00 和午夜之间:设置fromDate为tomorrow'sdate ,并toDate设置为tomorrow's date + 7 days


如何比较 currentTime 是否大于当地时间下午 5 点?


let currentTime = new Date().toLocaleTimeString('en-US', {

  hour: '2-digit',

  minute: '2-digit',

  second: '2-digit',

  hour12: false

}); 

 


守候你守候我
浏览 327回答 2
2回答

拉丁的传说

您可以使用options参数.toLocaleString将日期格式设置为“MM/DD/YYYY”var currentDate = new Date(Date.now() + (8 * 86400000))var newDateOptions = {        year: "numeric",        month: "2-digit",        day: "2-digit"}var newDate = currentDate.toLocaleString("en-US", newDateOptions );console.log(newDate)有关如何使用参数的详细帖子.toLocaleString- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

POPMUISE

var currentD = new Date();var startHappyHourD = new Date();startHappyHourD.setHours(17,30,0); // 5.30 pmvar endHappyHourD = new Date();endHappyHourD.setHours(18,30,0); // 6.30 pmconsole.log("happy hour?")if(currentD >= startHappyHourD && currentD < endHappyHourD ){    console.log("yes!");}else{    console.log("no, sorry! between 5.30pm and 6.30pm");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript