Home Web Front-end JS Tutorial How to convert between timestamp and date format using js

How to convert between timestamp and date format using js

Jun 21, 2018 pm 02:43 PM
js date format Timestamp

This article mainly introduces the conversion between js timestamp and date format in detail. It has certain reference value. Interested friends can refer to it.

The examples in this article share with you Android The specific code for displaying the Jiugongge picture is for your reference. The specific content is as follows

Convert timestamp to date format

var date = new Date(时间戳);获取一个时间对象。
下面是获取时间日期的方法
下面是获取时间日期的方法。
date.getFullYear();//获取完整的年份(4位,1970)
date.getMonth();//获取月份(0-11,0代表1月,用的时候记得加上1)
date.getDate();//获取日(1-31)
date.getTime();//获取时间(从1970.1.1开始的毫秒数)
date.getHours();//获取小时数(0-23)
date.getMinutes();//获取分钟数(0-59)
date.getSeconds();//获取秒数(0-59)
Copy after login

For example, I need a format like 2015-8-24_8-24-30

function formatDate(datetime) {
  var year = datetime.getFullYear(),
  month = (datetime.getMonth() + 1 < 10) ? &#39;0&#39; + (datetime.getMonth() + 1):datetime.getMonth() + 1,
  day = datetime.getDate() < 10 ? &#39;0&#39; + datetime.getDate() : datetime.getDate(),
  hour = datetime.getHours() < 10 ? &#39;0&#39; + datetime.getHours() : datetime.getHours(),
  min = datetime.getMinutes() < 10 ? &#39;0&#39; + datetime.getMinutes() : datetime.getMinutes(),
  sec = datetime.getSeconds() < 10 ? &#39;0&#39; + datetime.getSeconds() : datetime.getSeconds();
  return year + &#39;-&#39; + month + &#39;-&#39; + day + &#39;_&#39; + hour + &#39;-&#39; + min + &#39;-&#39; + sec;
}
Copy after login

Here datatime is a Date object, formatted datetime = new Date(time); time is a timestamp.

Convert date format to timestamp

var strtime = &#39;2014-04-23 18:55:49:123&#39;;
var date = new Date(strtime); 
//传入一个时间格式,如果不传入就是获取现在的时间了,这样做不兼容火狐。
// 可以这样做
var arr = strtime.replace(/ |:/g, &#39;-&#39;).split(&#39;-&#39;);
date = new Date(Date.UTC(arr[1], arr[2], arr[3], arr[4], arr[5]));

//三种方式获取:
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);

//三种获取的区别:
第一、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
比如上面代码输出的结果(一眼就能看出区别):
  1398250549123
  1398250549123
  1398250549000
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use iframe elements in vue components

How to implement nav navigation bar using vue

How to scroll up the web page express

The above is the detailed content of How to convert between timestamp and date format using js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Best Practices for Timestamp Obtaining: A Powerful Tool in Golang Programming Best Practices for Timestamp Obtaining: A Powerful Tool in Golang Programming Dec 29, 2023 am 08:28 AM

Golang Programming Tool: Best Practices for Timestamp Obtaining Introduction: In software development, timestamp is a very common concept. It is a numeric value that identifies the occurrence of a specific event, usually representing the number of milliseconds or nanoseconds since some reference point in time. In Golang, processing timestamps is very simple and efficient. This article will introduce the best practices for obtaining timestamps in Golang and provide specific code examples. Text: Get the current timestamp In Golang, getting the current timestamp is very simple. we can

See all articles