In JS, how can I get the date to format to MM/DD/YYYY?
new Date(Date.now() + (8 * 86400000)).toLocaleString().split(',')[0])
returns "12/1/2020"
How can I format it to "12/01/2020"?
fromDate:
(new Date(Date.now() + (1 * 86400000)).toLocaleString().split(',')[0]),
toDate:
(newDate(Date.now() + (8 * 86400000)).toLocaleString().split(',')[0])
I would like the fromDate and toDate to be:
If between 5:00 PM MST and Midnight: set fromDate
to tomorrow's
date , and toDate
to tomorrow's date + 7 days
How can compare the currentTime to say if it is greater than 5 PM local time?
let currentTime = new Date().toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
In JS, how can I get the date to format to MM/DD/YYYY?
new Date(Date.now() + (8 * 86400000)).toLocaleString().split(',')[0])
returns "12/1/2020"
How can I format it to "12/01/2020"?
fromDate:
(new Date(Date.now() + (1 * 86400000)).toLocaleString().split(',')[0]),
toDate:
(newDate(Date.now() + (8 * 86400000)).toLocaleString().split(',')[0])
I would like the fromDate and toDate to be:
If between 5:00 PM MST and Midnight: set fromDate
to tomorrow's
date , and toDate
to tomorrow's date + 7 days
How can compare the currentTime to say if it is greater than 5 PM local time?
let currentTime = new Date().toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
Share
Improve this question
edited Nov 24, 2020 at 4:47
Bhoomiputra
asked Nov 24, 2020 at 1:02
BhoomiputraBhoomiputra
711 gold badge2 silver badges11 bronze badges
1
|
2 Answers
Reset to default 15You can use the options
argument in .toLocaleString
to format your date as "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)
A detailed post on how to use the arguments in .toLocaleString
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
This from another post here.
var currentD = new Date();
var startHappyHourD = new Date();
startHappyHourD.setHours(17,30,0); // 5.30 pm
var endHappyHourD = new Date();
endHappyHourD.setHours(18,30,0); // 6.30 pm
console.log("happy hour?")
if(currentD >= startHappyHourD && currentD < endHappyHourD ){
console.log("yes!");
}else{
console.log("no, sorry! between 5.30pm and 6.30pm");
}
en-us
locale, with 2 digit month and day, and numeric year – Bravo Commented Nov 24, 2020 at 1:14