Click here to Skip to main content
15,611,306 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"
    document.getElementById('time_2').innerHTML = date_and_time;

Updated 29-Jul-21 22:35pm

Share this answer
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 :-)
Share this answer

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