Incorrect spacing in the toLocaleTimeString method in Microsoft Edge's JavaScript implementation

I'm struggling to comprehend the behavior of a JavaScript code that appears to function differently in Edge. Here's what I've narrowed it down to:

var testi = new Date().toLocaleTimeString();
var len2 = testi.length;
alert(len2);

In Edge, the length is 17, while in Chrome and IE it's 10. It seems like there are hidden spaces in the string, causing issues with substring operations.

https://jsfiddle.net/m1m8h7ym/

Just so you know, my Time Zone is set to US Central.

Answer №1

It seems that Microsoft is experiencing issues with the invisible left-to-right mark, which is also affecting IE11 in Edge mode. The solution involves looping through each character in the string and using encodeURIComponent() to identify the problematic characters.

var output = document.getElementById("output");
var testi = new Date().toLocaleTimeString();
var row;
for (var i = 0, len = testi.length; i < len; i++) {
    row = document.createElement("pre");
    row.innerHTML = encodeURIComponent(testi[i]);
    output.appendChild(row);
}
<div id="output"></div>

The issue can be resolved by removing the left-to-right mark using its unicode representation, which can be achieved with a regex expression like \u200E.

var output = document.getElementById("output");
var testi = new Date().toLocaleTimeString().replace(/\u200E/g,"");
var row;
for (var i = 0, len = testi.length; i < len; i++) {
    row = document.createElement("pre");
    row.innerHTML = encodeURIComponent(testi[i]);
    output.appendChild(row);
}
<div id="output"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Send data from a form in an HTML document to a PHP file and then showcase the outcome within the identical HTML

Seeking assistance, I have created an HTML file with 2 tabs. The first tab contains a form that takes user input and performs a function. I aim to display the result of this function in the second tab. The form in the first tab needs to submit an action to ...

Troubleshooting UV Mapping Problem in THREEJS JSON Model

After reviewing the json file for a 3D model, I came across the following line: "uvs": [[-3.21834,-1.65399,-3.21834,-2.57657,4.21834,-2.57657,4.21834,-1.65399,4.21834,-1.85233,4.21834,-2.7749,-3.21834,-2.7749,-3.21834,-1.85233,4.21834,0.961286,-3.21834,0 ...

The appearance of the image is distorted once it is inserted into the carousel

I am currently working on a carousel that contains 5 images with varying heights and widths. However, I am experiencing some issues with the way the images are displayed - some are stretched while others are centered within the carousel. My goal is to ens ...

The value within the style.setProperty function does not get applied

I have a progress bar that dynamically increases based on user input: <div class="progressBarContainer percentBar"> <div class="progressBarPercent" style="--width:${gPercent}" id="${gName}-pbar">< ...

What could be causing the warning message about 'bodyParser' being deprecated to appear on my screen?

Even though I used the most recent versions of both packages, I am still receiving a warning that 'bodyParser' is deprecated. It doesn't seem to affect my code, but I'm curious as to why this warning is appearing. const express = requi ...

Assign a variable to a dynamic model within an AngularJS scope

I am facing an issue with scope closure. ... function(scope) { ... }); Within the scope, I need to achieve the following: $scope.favoriteTitle = 'some value' The challenge is that favoriteTitle cannot be static. It needs to be generated d ...

Pair of dimensions painting with d3 version 4

I am having trouble converting my code from d3 v3 to d3 v4 Below is the original code snippet: var brush = d3.svg.brush() .x(x) .y(y) .on("brushstart", brushstart) .on("brush", brushmove) .on("brushend", brushend); However ...

Having difficulty encapsulating a dynamically generated "p" tag and "img" tag within a dynamically generated "div" tag

As I attempt to encapsulate the image tag and title tag inside the dynamically created div tag, I encounter an unusual error message stating "TypeError: Assignment to constant variable.at Array.forEach"! Can someone please point out what mistake I am mak ...

Display a pop-up when hovering over a layer with react-leaflet

I am attempting to display a popup when hovering over a layer in react leaflet. Utilizing GeoJson to render all layers on the map and onEachFeature() to trigger the popup on hover, I encountered an issue where the popup only appeared upon click, not hover. ...

Guide for using two Async Pipe functions in Angular 7

Two different functions are in place to check a specific condition, and the requirement is for both of them to be true simultaneously. How can *ngIf be utilized to achieve this? Currently, setting just one of them works, but the aim is to have both. HTML ...

Learn how to navigate to another page after successful AJAX request handling

Upon deletion of a file, I am attempting to redirect to the admin_dashboard page using ajax. I have a function called in a button tag, and upon successful response, I want to redirect to the next page. While the value gets deleted successfully, I am facing ...

Node.js Sequelize QueryExplore the power of Sequelize in Node.js

I'm looking to filter the "incomers" based on age, but all I have in the table is their date of birth. I want to find people within a specific age range, how can I accomplish this? router.post('/', function (req, res, next) { let parame ...

Notification within the conditional statement in React JS

I am working on validating phone number input within a React JS component using an if/else statement. If the user enters letters instead of numbers, I want to display a message saying "please check phone number". While I have been able to create a function ...

``Is it possible to adjust the style of an HTML element based on the style of another element?

Within my web application, I am dynamically changing the style of an HTML element using JavaScript. Below is the code snippet: function layout() { if(document.getElementById('sh1').getAttribute('src') == "abc.png") { docum ...

Having trouble retrieving and displaying data from Mongo in Meteor with React

Seeking assistance with a React and Meteor issue. I am using TrackerReact to populate data in Mongo, but I am unable to display it in another component. When I console.log the data after the page loads, I see that the Mongo data loads after the page, causi ...

Send data using only Javascript

Hey there, I'm a beginner in javascript and I'm having some trouble submitting a form using pure javascript. Here is my code: var myform = document.getElementById('js-post-form'); myform.addEventListener('submit', function(e ...

Is there a feature in Angular JS similar to dojo.hitch()?

I apologize for my lack of knowledge in AngularJS. I am wondering if there exists a similar function to dojo.hitch() within AngularJS. dojo.hitch() returns a function that is ensured to be executed in the specified scope. ...

PHP query for beginners. Integrating JavaScript into a PHP webpage

Hey there! I am not familiar with PHP development, as I have never worked with it before. However, I have been tasked with adding a Google Shopping cart tracking code to a website. After someone completes an order, they are sent to finishorder.php. Upon re ...

Switch the view to a grid layout upon clicking

Using bootstrap 5, I've successfully created a list view: <div class="col"> Click to switch to grid/list </div> Below is the content list: <div class="row mt-3 list"> list view ... ..... ....... </div ...

Utilizing VueJs (Quasar) to access vuex store within the router

Recently, I have been diving into learning Vuex and creating an authentication module. Following a tutorial, I reached a point where I needed to use the store in the router. However, after importing my store at the top of the router index.js file, I notice ...