Home  >  Article  >  Web Front-end  >  Analysis of conversion method between time string and timestamp in javascript

Analysis of conversion method between time string and timestamp in javascript

不言
不言Original
2018-09-14 15:51:211608browse

The content of this article is about the analysis of the conversion method between time string and timestamp in JavaScript. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Problems to be solved

JS development encountered a problem: the time string format generated by the UI component of vue on the front end is 2018-9-12 9:11:23, but the backend can only recognize it 2018-09-12 09:11:23 Such a time string means that the time string format needs to be converted, and 0 must be added in front of the single-digit value.

Solution

  1. Convert the existing time string to a timestamp first

  2. Create a Date object using the timestamp , and then use the Date method to obtain the corresponding year, month, day, hour, minute, and second values

  3. Add 0, and generate the required time string

Involving knowledge points

Time stamp concept: the number of milliseconds from 0:00 on January 1, 1970 to the specified date (string).
You need to use the Date object in JS to process date and time. The methods used are as follows:

Date.parse(timestr): 返回timestr的时间戳
var time = newDate(timestamp)   利用时间戳创建Date对象

For methods of JavaScript Date objects, please refer to: http://www.w3school.com.cn/js. ..
The time string is assembled on demand. You need to find a way to add 0. There are many methods on the Internet.

The following is the conversion process:

Convert time string to timestamp

//mytime是待转换时间字符串,格式:'2018-9-12 9:11:23'
dateTmp = mytime.replace(/-/g,'/')   //为了兼容IOS,需先将字符串转换为'2018/9/11 9:11:23'
timestamp = Date.parse(dateTmp)    //返回'2018-9-12 9:11:23'的时间戳

Convert timestamp to time string in required format

    var dateFormat = function(timestamp){
        var time = newDate(timestamp)    //先将时间戳转为Date对象,然后才能使用Date的方法
        var year = time.getFullYear(),
            month = time.getMonth() + 1 ,  //月份是从0开始的
            day = time.getDate(),
            hour = time.getHours(),
            minute = time.getMinutes(),
            second = time.getSeconds()
            //add0()方法在后面定义
        return  year+'-'+this.add0(month)+'-'+ this.add0(day)+''+this.add0(hour)+':'+this.add0(minute)+':'+this.add0(second)  
        }

How to add 0 add0

There are many ways to add 0 on the Internet. The method I use is as follows:

        var add0 = function(m){
        return m < 10 ? '0' + m: m
        }

Summary

You need to understand the Date object in JavaScript.
To understand the idea of ​​string conversion: that is, string--timestamp---Date object---save a new string, once you have the idea, it will be a matter of course.

Related recommendations:

js gets the time and converts between strings and timestamps_javascript skills

The above is the detailed content of Analysis of conversion method between time string and timestamp 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