Home  >  Article  >  Web Front-end  >  How to convert date to number in javascript

How to convert date to number in javascript

藏色散人
藏色散人Original
2023-02-10 09:12:313722browse

Javascript method to convert date to number: 1. Create a js sample file; 2. Encapsulate a changeTime function through "function changeTime(option) {var currentTime = option;...}"; 3. Through Just call the changeTime function to implement date conversion.

How to convert date to number in javascript

The operating environment of this tutorial: Windows 10 system, javascript version 1.8.5, Dell G3 computer.

How to convert date to number in javascript?

js changes the date format into numbers

When I was working on a project today, I came across the idea of ​​changing the time format into numbers, so I checked it online. How to remove symbols between strings, but there are three symbols in my time: "-", ":", and spaces, so I tried it three times. I didn't find any faster method, so I just tried it for the time being. Let’s achieve the effect like this first. If there is a better way in the future, it’s okay to change it.

Here I have encapsulated a function, just call it wherever it is useful:

function changeTime(option) {
       // currentTime当前时间,假设是 2019-7-2 19:03
       var currentTime = option;
       var reg = new RegExp("-","g");//去掉时间里面的-
       var a = currentTime.replace(reg,"");
       var regs = new RegExp(" ","g");//去掉时间里面的空格
       var b = a.replace(regs,"");
       var regss = new RegExp(":","g");//去掉时间里面的:冒号
       var c = b.replace(regss,"");
       console.log('时间变成数字的结果:',c)
}
changeTime('2019-7-2 19:03');//函数调用

Related expansion:

replace() method is used in strings Replace some characters with other characters, or replace a substring that matches a regular expression.

Syntax

stringObject.replace(regexp/substr,replacement)

Parameters

regexp/substr

Required. A RegExp object that specifies the substring or pattern to be replaced.

Note that if the value is a string, it is retrieved as a literal text pattern rather than first being converted to a RegExp object.

replacement Required. A string value. Specifies functions for replacing text or generating replacement text.

Return value

A new string obtained by replacing the first match or all matches of regexp with replacement.

Explanation

The replace() method of string stringObject performs a search and replace operation. It will look for substrings in stringObject that match regexp and replace those substrings with replacement. If the regexp has the global flag g, then the replace() method replaces all matching substrings. Otherwise, it only replaces the first matching substring.

replacement can be a string or a function. If it is a string, then each match will be replaced by the string. But the $ character in replacement has a specific meaning. As shown in the following table, it illustrates that the string obtained from the pattern match will be used for replacement.

Recommended learning: "JavaScript Video Tutorial"

The above is the detailed content of How to convert date to number in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn