In formatting time js

Development tools and key technologies: VS ASP.NET.MVC
Author: slowly
compose Time: June 25, 2019

Sometimes in the page you need to get to the exact date and then (when minutes and seconds) this format to date on the page, but without the date or time formatting is not displayed in this format, it shows just a string.
If the time to return the JSON type directly there will be a problem, as shown below:
Here Insert Picture Description
1
Time after being serialized JSON type into a date of birth of the figure above, the presence time of birth, in the form as enrollment time, does not look like it is time to type. So we have to solve this problem. To solve this problem there are two ideas, one is in the format of the page is processed; the other is processed directly in the controller.
Now writing is the format for processing in the page:
First talk about the type of relationship: database date and datetime types are mapped to C # is DateTime type (year, month, day, hour, minute, and second there); database timestamp time and the type is mapped to the C # TimeSpan type (mainly date state, is represented by the time difference).
By custom method for formatting the time type, FIG follows:
Here Insert Picture Description
2
Time JSON format type to pass in the Date and then replaced by an empty replace method, which corresponds to FIG 1 / Date (929462400000) / Digital to take part out; the second step that is positive or negative is determined indexOf date for special symbols appearing in a cutting some special cases ( "+" "-"), it is difficult to occur under normal circumstances may occur, however, if this is the case we will put in the "+" or "-" specific figures before taken out.
Next, turn it into Date Type: first with parseInt converted into integer, and then pass it to turn into a date Date class type. It acquired several months, and days, months, and if less than 10 days prior then the number of days plus 0 months or, if more than 10 returns to normal. And then began to concatenate strings, use "-" segmentation. If isDateTime final judgment is not empty, undefined or it does not mean it is true then stitching up the minutes and seconds, or if no false isDateTime would not splice minutes and seconds.
Then call this method:
Here Insert Picture Description
FIG. 3
results in the following figure:
Here Insert Picture Description
FIG. 4
there is a more specific: that is, return TimeSpan type, this is with respect to two or more, more simply. The above two types need to be converted into a date and then splicing the string, and this does not need, the string can be spliced directly. FIG follows:
Here Insert Picture Description
FIG. 5
a little above two are the same, is determined after acquiring the value is greater than 10, greater than the current value is output, otherwise add "0" in front of the current value. Results as shown below:
Here Insert Picture Description
Figure 6
In this way, the js formatting time is complete, if there are places not written or have other questions, please comment in the comments section, I will promptly corrected!

Guess you like

Origin blog.csdn.net/weixin_44560796/article/details/93753654