Immediately make a reservation that with JS I'm on you. On the Internet, I found an option how to change the time (for example, a comment, or publication of an article), according to the user's time offset, based on it wrote a script. Actually the script itself:

var date_with_offset = new Date().getTimezoneOffset() * 60000; $(".datetime-convert").each(function() { var offsetDate = new Date(parseDatetime($(this).text()).getTime() + -date_with_offset); $(this).text(offsetDate.toLocaleString()); }); function parseDatetime(value) { var a = /^(\d{2}).(\d{2}).(\d{4}) (\d{2}):(\d{2}):(\d{2})$/.exec(value); if (a) { return new Date(+a[3], +a[2] - 1, +a[1], +a[4], +a[5], +a[6]); } return null; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="datetime-convert">24.02.2017 12:00:00</div> 

As you can see, the date is displayed in the format: 02/24/2017 12:00:00.

Is it possible to display this way: February 24, 2017 12:00:00. And if so, how?

    1 answer 1

    To change the formatting of the date, you need to specify additional. parameters for toLocaleString (); Link to the tutorial with dates

     let el = document.querySelector('.date-convert'); let elDate = el.textContent; const timeOffset = new Date().getTimezoneOffset() * 60000; let brokenDate = elDate.split(' ')[0].split('.'); let time = elDate.split(' ')[1]; let fixedDate = `${brokenDate[1]}.${brokenDate[0]}.${brokenDate[2]} ${time}`; let parsedDate = Date.parse(fixedDate) + -timeOffset; const options = { year: 'numeric', month: 'long', day: 'numeric', timezone: 'UTC', hour: 'numeric', minute: 'numeric', second: 'numeric' }; el.textContent = new Date(parsedDate).toLocaleString('ru', options) 
     <div class="date-convert">24.02.2017 12:00:00</div> 

    Here is the solution with jquery.

     $('.date-convert').each(function() { convertDate(this); }); function convertDate(el) { let elDate = el.textContent; const timeOffset = new Date().getTimezoneOffset() * 60000; let brokenDate = elDate.split(' ')[0].split('.'); let time = elDate.split(' ')[1]; let fixedDate = `${brokenDate[1]}.${brokenDate[0]}.${brokenDate[2]} ${time}`; let parsedDate = Date.parse(fixedDate) + -timeOffset; const options = { year: 'numeric', month: 'long', day: 'numeric', timezone: 'UTC', hour: 'numeric', minute: 'numeric', second: 'numeric' }; el.textContent = new Date(parsedDate).toLocaleString('ru', options); } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="date-convert">24.02.2017 12:00:00</div> <div class="date-convert">24.02.2017 15:00:00</div>