Thinking about the formatting of data text in JavaScript

Source: Internet
Author: User

In some scenarios where accuracy is not as accurate as possible, we can directly Number.prototype.toFixed() implement the need to retain two decimal places.

var num = 123.45678console.log(num.toFixed(2)) //123.46var num2 = 12console.log(num2.toFixed(2)) //12.00

However, if it happens that the number is an integer, then 12.00 the format is output, and we often require that the integer be 00 output directly for the integer that follows. So write it that way.

var num=123.45678console. Log (num.tofixed (2).  //123.46var num2 = 12console. Log (num2. ToFixed (2).  Replace ( //12            

toFixed() replace() Replace the string that appears immediately after the integer .00 .

PS: Number.prototype.toFixed The return is a string

The number is [0-9] in the case of the pre-complement 0

In the case of outputting some numbers, if it is less than ten , you need to fill in the front 0, especially when the output date time.

Before using the Date object to obtain the relevant time data to determine whether it is less than ten, if it is to fill 0.

var date = new Date()var min = date.getMinutes()min = min < 10 ? ‘0‘ + min : minconsole.log(min) //08

Later I felt that it was not elegant enough, and the code was so numerous that I thought of replacing it with strings.

var date = new Date()var min = String(date.getMinutes()).replace(/^(\d{1})$/, ‘0$1‘)console.log(min) //08

This makes use of regular to match to single-digit case directly in front plus 0 , a line of code, more elegant.

To continue with the derivation, I basically have to do in the date format when the number needs to be replaced, why not directly replace the entire string? For example, it will be 2017-1-8 12:8 replaced 2017-01-08 12:08 .

var date = ‘2017-1-8 12:8‘.replace(/\b\d{1}\b/g, ‘0$&‘)console.log(date)

The entire string substitution is done through regular, no longer targeted to some parts of the processing. Finally, a complete example of the formatted date function is given.

function FormatDate(Source,Format= ' Yyyy-mm-dd '){Let date=NewDate ();if (typeof Source===' String ') format= Source;if (typeof Source===' Number ') date=NewDate (source);if (typeof Source===' Object ') date= Source;Const year=Date.getFullYear ();Const MONTH=Date.GetMonth ()+1;Const DAY=Date.GetDate ();Const HOUR=Date.GetHours ();Const Miniute=Date.Getminutes ();Const second=Date.Getseconds ();ReturnFormat.Replace ' yyyy '  Replace ( ' MM ' , month).  Replace ( ' DD '  Replace ( ' hh '  Replace ( ' mm '  Replace ( ' SS '  Replace (/\b\d{1}\b/g< Span class= "hljs-string" > ' 0$& ') ;}         

Thinking about formatting data text in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.