IE 11 encountering issues with Date.parse returning NaN

Whenever I attempt to parse a date in Internet Explorer 11, it throws NaN at me. However, in Chrome and Firefox, I get the timestamp 1494559800000.

Date.parse("5/12/2017 09:00 AM")

The condition below is where things go wrong for me in IE 11. Is there an alternative library or method that can help me resolve this issue in IE 11?

tArray includes ["09:00 AM", "05:00 PM"];

var tArray = timings.toUpperCase().split('-');
var timeString1 = currentDate.toLocaleDateString() + " " + tArray[0];
var timeString2 = currentDate.toLocaleDateString() + " " + tArray[1];
var currentTimeString = currentDate.toLocaleDateString() + " " + currentTime.toUpperCase();
//The problematic condition resides here.
if (Date.parse(timeString1) < Date.parse(currentTimeString) 
                 && Date.parse(currentTimeString) < Date.parse(timeString2)) {

I have set up a sample fiddle to demonstrate where it fails. https://jsfiddle.net/vwwoa32y/

Answer №1

Based on information from the MDN, when using the Date.parse() method:

dateString

It should be a string representing an RFC2822 or ISO 8601 date (although other formats are possible, results may not be as expected).

Seems like Microsoft did not support the format you provided. It's recommended to avoid this format anyway due to its locale dependency (which can vary between dd/mm/yyyy or mm/dd/yyyy).

An alternative approach is to utilize moment.js. This library offers extensive functionality for handling dates and times. Here are some examples of how it could be used:

//Create a moment instance with the current date and time
var now = moment();

//Parse a specific date and time using a custom format
var specificTime = moment('5‎/‎12‎/‎2017 09:00 AM', 'DD/MM/YYYY hh:mm a');

//Check if the specific date and time is before the current date and time
var beforeNow = specificTime.isBefore(now);

Edit: I have rewritten your code using version 2.18.1 of moment.js. Here is the updated version:

function parseDateCustom(date) {
    return moment(date, 'YYYY-MM-DD hh:mm a');
}

var tArray = ["09:00 AM", "05:00 PM"];
var currentDate = moment().format('YYYY-MM-DD') + ' ';
var timeString1 = parseDateCustom(currentDate + tArray[0]);
var timeString2 = parseDateCustom(currentDate + tArray[1]);
var currentTimeString = parseDateCustom(currentDate + "01:18 pm");

if (timeString1.isBefore(currentTimeString) && currentTimeString.isBefore(timeString2)) {
    console.log('Success');
} else {
    console.log('Failed');
}

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

Understanding the default value type in React's setState method

I am new to React and facing difficulties identifying errors in my code. My goal is to display a list of users retrieved from a server using socket.io. Below is the original code snippet: // list of users const [users, setUsers] = useState() useEffect(() ...

Tips for including multiple directives in a component

Apologies in advance for my lack of clarity in expressing this question, which is why I am seeking assistance rather than finding the solution on my own. My query pertains to loading a component within another one and specifying it in the directive. Below ...

Include a requirement within an application/ld+json formatted script

I have a task to create an application using a script in the js+json format. It is crucial to include the person schema, which signals to Google and other search engines how to effectively interpret the structure of the page and its content. My current cod ...

Do I have to cram all the content onto a single page just to use a scroll effect?

I'm currently facing a dilemma as I work on building my portfolio. My goal is to primarily use html5 and css3, along with a bit of js, jquery, and other tools if necessary. Although I am not an expert in web development, I wanted to push myself to cre ...

The Chrome browser does not recognize Sys.WebForms

I am encountering an issue with my Web-Application when trying to perform partial updates. The error message "Sys.WebForms is undefined in Chrome (latest version)" keeps popping up. Despite my extensive research efforts, I have not been able to find a solu ...

How can you determine if a mouseover event is triggered by a touch on a touchscreen device?

Touchscreen touches on modern browsers trigger mouseover events, sometimes causing unwanted behavior when touch and mouse actions are meant to be separate. Is there a way to differentiate between a "real" mouseover event from a moving cursor and one trigg ...

Executing javascript functions from various HTML tags

The code snippet below is what I currently have: <script src="jquery-1.10.2.min.js"></script> <script> $('#year li').click(function() { var text = $(this).text(); //alert('text is ' + text); $.post("B.php" ...

Ensure modifications to a variable are restricted within an if statement

I am struggling to find a way to globally change a variable within an if statement and ensure that the modifications persist. User input: !modify Bye var X = "Hello" if (msg.content.includes ('!modify')) { X = msg.content.replace('!modi ...

Reactjs encountering issues with function of Cookies section

Currently, I am working on a login module in Reactjs using Nextjs. Upon successful login, I am storing the user email in a cookie. The functionality is working as expected, but I want to restrict access to the login page for users who are already logged in ...

JavaScript function not executing

Within a panel in an UpdatePanel, there is both a dropdown list and a file upload control. The goal is to enable the Upload button when a value is selected from the dropdown and a file is uploaded. This functionality is achieved through a JavaScript functi ...

Using Vue.js to loop through data objects when a button is clicked

I'm currently working on building a quiz functionality using vue.js, but I've hit a roadblock in trying to make the "Next" button cycle through my data. The goal is to have the screen display the object containing the question and answers (e.g. q ...

Encountering difficulties resolving the dependency tree while trying to install @react-navigation/[email protected]

After creating a new project called MyProject using react-native init MyProject, I opened it in VSCode and the first task was to install navigation. Initially, when running npm install @react-navigation/native @react-navigation/stack, it threw an error. S ...

How can one identify a concealed glitch that exclusively occurs for a particular individual or hardware in a React environment?

Is it possible to identify a bug that occurs only with a particular individual or hardware in a React application? This bug is invisible and never appears during tests, but only manifests with a specific client within my company. Do you have any tips on h ...

Positioning Multi-level Drop Down Div in Javascript - How to do it efficiently?

I'm currently working on a horizontal menu using CSS and JavaScript with multiple levels. I've implemented a toggle function to show the submenu container, but it's causing the links below it to be pushed down. Is there a way to make the dis ...

What is the best way to filter out empty arrays when executing a multiple get request in MongoDB containing a mix of strings and numbers?

I am currently working on a solution that involves the following code: export const ProductsByFilter = async (req, res) => { const {a, b, c} = req.query let query = {} if (a) { query.a = a; } if (b) { query.b = b; } if (c) { ...

Tips for resolving the "unsafe-eval" issue in Vue3 on the client-side platform

My app is built using Express, cors, and helmet. I have incorporated Vue3 on the client-side only, with the library file being self-hosted in the public folder. To add the module to the client-side, I simply include the following script: <script type=&q ...

Material-UI's style is taking precedence over other styles that have been defined

Introduction Last week, I posted a similar query which touched on the same issue. However, as the solution seems to be different this time around, I am revisiting it in a new thread. Check out the updated version of the CodeSanbox Example that reflects t ...

Display various formats in a pop-up window when different buttons are clicked

There has to be a simpler way to handle this situation. Currently, I have a modal with 4 hidden forms, one of which is active based on the "active" class. I want to show each form when different buttons like sign-up, forgot password, or sign-in are clicked ...

Execute the javascript function asynchronously

I need to call the constGrid(arg1) function 3 times to set up an extjs grid when my form loads. There are other fields on my page as well. I want to ensure that the page does not hang or wait for the method to complete. onLoad(function() { for (var i= ...

How to clean a string from PHP code using jQuery

Looking for a solution to extract PHP code from a string. The string I have contains PHP code that needs to be removed. text = '<?php // This is a test sample ?> This is a description'; text.replace(/\<\?.*\?\?\ ...