Found 6710 Articles for Javascript

How to calculate the difference between two dates in JavaScript?

Jennifer Nicholas
Updated on 17-Jun-2020 06:27:09

2K+ Views

To get dates in JavaScript, use the getTime() method. Forgetting the difference between two dates, calculate the difference between date and time.ExampleYou can try to run the following code to learn how to calculate a difference between two dates −Live Demo                    var dateFirst = new Date("11/25/2017");          var dateSecond = new Date("11/28/2017");          // time difference          var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());          // days difference          var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));          // difference          alert(diffDays);          

What is the shortest function of reading a cookie by name in JavaScript?

Rishi Rathor
Updated on 16-Jun-2020 12:11:58

144 Views

Here’s the best way to read cookies by name in JavaScript. The following is the function −function ReadCookie() {    var allcookies = document.cookie;    document.write ("All Cookies : " + allcookies );    // Get all the cookies pairs in an array    cookiearray = allcookies.split(';');    // Now take key value pair out of this array    for(var i = 0; i < cookiearray.length; i++) {       name = cookiearray[i].split('=')[0];       value = cookiearray[i].split('=')[1];       document.write ("Key is : " + name + " and Value is : " + value);    } }

How to generate random numbers between two numbers in JavaScript?

Kumar Ishan
Updated on 05-Sep-2022 07:19:27

12K+ Views

To generate a random number, we use the Math.random() function. This method returns a floating-point number in the range 0 (inclusive) to 1 (exclusive). To generate random numbers in different range, we should define the minimum and maximum limits of the range. Please follow the second syntax below for this. To generate random integers, you can follow the third and fourth syntax discussed below. As the Math.random() method gives us a random floating point number so to convert the result into an integer, we need to apply some basic math. Please follow the below syntaxes. Syntax Following is the syntax ... Read More

How to get time in milliseconds since the Unix epoch in JavaScript?

Krantik Chavan
Updated on 24-Sep-2019 08:30:59

1K+ Views

The standard Unix epoch is an expression in seconds. The Unix epoch or Unix time is the number of seconds elapsed since January 1, 1970.The getTime() method is used to return the millisecond representation of the Date object.To get time in milliseconds, use the following:var milliseconds = (new Date).getTime();

How to get a number value of a string in Javascript?

Nancy Den
Updated on 17-Jun-2020 06:24:14

532 Views

To get an integer in the string “abcde 30”, use the following regex in JavaScript, else it will return NaN. With that, use parseInt() to get the numbers with regex under String match() method −ExampleLive Demo                    var myStr = "abcdef 30";          var num = parseInt(myStr.match(/\d+/))          alert(num);          

How to get a string representation of a number in JavaScript?

Daniol Thomas
Updated on 17-Jun-2020 06:23:34

474 Views

Use the toString() method to get the string representation of a number. You can try to run the following code to print a string representation of numbers like 20, -40, 15.5 −ExampleLive Demo                    var num1 = 25;          document.write(num1.toString()+"");          document.write((30.2).toString()+"");          var num2 = 3;          document.write(num2.toString(2)+"");          document.write((-0xff).toString(2));           Output25 30.2 11 -11111111Above, you can see we added a parameter to the toString() method. This is an optional parameter, which allows you to add an integer between 2 and 36, which is the base for representing numeric values.

How to create a session only cookies with JavaScript?

Vrundesha Joshi
Updated on 16-Jun-2020 12:15:34

1K+ Views

To create a session only cookie, you need to set the expiration date of the cookie to be some years ahead. You need to set the expire attribute −current time + 5 yearsThe expire attribute shows the date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.Note − Do not consider removing the expire value to create a session only cookie. This won’t work. Set the expire attribute to the future date.

What is a NaN property of a Number object in JavaScript?

Saurabh Jaiswal
Updated on 05-Jan-2023 15:27:18

639 Views

In JavaScript, the NaN property is a special value that represents "Not a Number". It is a property of the Number object and can be accessed using Number.NaN. The NaN property is usually produced as a result of an operation that cannot produce a meaningful result. For example, dividing 0 by 0 or trying to parse an invalid number will both produce NaN. Here are a few examples of operations that will produce NaN − Math.sqrt(-1); // NaN 0/0; // NaN parseInt("foo"); // NaN It ... Read More

How to store large data in JavaScript cookies?

Nishtha Thakur
Updated on 16-Jun-2020 12:03:27

683 Views

To store large values in JavaScript cookies, try the following possibilities −Create a "session id" and save the settings in a database. Then store the session id in the cookie.Store the indexes of the selected items. Let’s say the selected items are the first 20 items on the list −P[0,19]Create a table to store cookies data such as −[ data_id , data , data_create , date_expire ]Store data_id in a table and when a user visits, get the data_id in cookies.

How can I list all cookies on the current page with JavaScript?

Smita Kapse
Updated on 16-Jun-2020 12:01:38

1K+ Views

While creating a cookie, use the “path” parameter. The path to the directory or web page set the cookie. This may be blank if you want to retrieve the cookie from the current page. By default, the cookie belongs to the current page.ExampleTo list all cookies for the current page, try the following code −Live Demo                    function ReadCookie() {             var allcookies = document.cookie;             document.write ("All Cookies : " + allcookies );             // Get all the cookies pairs in an array             cookiearray = allcookies.split(';');             // Now take key value pair out of this array             for(var i=0; i

Advertisements