Click here to Skip to main content
15,040,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I get only date and time separately using tolocalestring with date in javaScript.


What I have tried:

function update_time_zone_2() {
    let timezone = time_zones_2.options[time_zones_2.selectedIndex].value;

    let date_and_time = new Date().toLocaleString("en-US", {
        timeZone: "Africa/Abidjan",
        timeStyle: "medium",
        hourCycle: "h12"
    });
    console.log(date_and_time);
    document.getElementById('time_2').innerHTML = date_and_time;

}
Posted
Updated 29-Jul-21 22:35pm

   
Comments
Arslan Anjum 2021 30-Jul-21 6:01am
   
Thanks, It is really very helpful.
dnow = Date().toLocaleString() - will give you all you want. Just break on space
dnow = dnow.split(" ", 9); Result: (9) ["Tue", "Aug", "10", "2021", "14:16:01", "GMT-0500", "(Central", "Daylight", "Time)"]
dnow[4]; will give you current time "14:16:01"

Two lines of coding and you are all set :-)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900