In-depth understanding of time js format_javascript in MVC

Source: Internet
Author: User
The following small series will bring you an in-depth understanding of the time js formatting in MVC. I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at one of my experiences. THE post request in MVC returns a JSON string that contains the time format in the database (for example,/Date (10000000000 )/), I don't know how to handle it.

Baidu's methods are not applicable. After self-research, we have made a Jquery plug-in, hoping to help you.

Plug-in source code:

(Function ($) {/format the Date type returned by JSON to its own Defined Format: for example, yyyy-MM-dd hh: mm: ssdtstr: Date returned by JSON "/Date (10000000000) /"* fmt: custom format, for example, yyyy-MM-dd hh: mm: ss/$. format = function (dtstr, fmt) {var date = $. toDateTime (dtstr); var o = {"M +": date. getMonth () + 1, // month "d +": date. getDate (), // Day "h +": date. getHours (), // Hour "H +": date. getHours (), // Hour "m +": date. getMinutes (), // minute "s +": date. getSeconds (), // second "q +": Math. floor (date. getMonth () + 3)/3), // quarter "S": date. getMilliseconds () // millisecond}; if (/(y + )/. test (fmt) fmt = fmt. replace (RegExp. $1, (date. getFullYear () + ""). substr (4-RegExp. $1. length); for (var k in o) if (new RegExp ("(" + k + ")"). test (fmt) fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length); return fmt;}/format JSON return Date type LONG to Date dtstr: JSON return Date "/Date (10000000000)/"/$. toDateTime = function (dtstr) {// 1900-01-01T00: 00: 00if (dtstr + ''). indexOf ('date (')> = 0) {var ldate = + (dtstr + ''). replace ('/Date (',''). replace (')/', ''); if (ldate> 0) {return new Date (ldate) ;}} else {var str = (dtstr + ''). replace (/-/g ,"/"). replace (/T/g, ""); if (str. Split ('. ') [0] = '2014/1/01 00:00:00') {return null;} else if (dtstr + ''). length> 0) {return new Date (str. split ('. ') [0]) ;}} return null;}/format JSON return date type LONG to convert the specified format string: 12: 59: 59 title: date type name "release time" dtstr: JSON return Date "/Date (10000000000)/" substrstart: String start truncation position "5"/$. toDateTimeString = function (title, dtstr, substrstart) {var dt = toDateTime (dtstr); if (dt) {var y = dt. getFullYear (); var m = dt. getMo Nth () + 1; var d = dt. getDate (); var h = dt. getHours (); var I = dt. getHours (); var s = dt. getHours (); var datestr = y + "-" + (m <10? "0": "") + m + "-" + (dt. getDate () <10? "0": "") + dt. getDate () + "+ (dt. getHours () <10? "0": "") + dt. getHours () + ":" + (dt. getMinutes () <10? "0": "") + dt. getMinutes () + ":" + (dt. getSeconds () <10? "0": "") + dt. getSeconds (); return title + datestr. substr (substrstart);} return "";}/format JSON return date type description: convert a LONG type date to a specified format string, evaluate the time difference between dates, and append it to the specified set: 12: 59: 59 htmls: target set title: Date type name "release time" dtstr: JSON return Date "/Date (10000000000)/" dtstr01: JSON return Date "/Date (10000000000) /"Time Difference end Date 1dtstr02: JSON return Date"/Date (10000000000)/"Time Difference end Date 2 if it is null take 1 substrstart: String start truncation position" 5 "*/$. toDateTimeDiffString = function (htmls, title, dtstr, dtstr 01, dtstr02, substrstart) {var dtstrs = toDateTimeString ("", dtstr, substrstart); if (dtstrs) {var dt1 = toDateTime (dtstr ); var dt2 = toDateTime (dtstr01); var dt3 = toDateTime (dtstr02); if (dt3) {dt2 = dt3;} var tstime = dt1-dt2; var m = Math. floor (tstime/1000/60, 0); var s = Math. ceil (tstime/1000% 60); htmls. push (''+ title + (m> 0? M + "points": "") + (s> 0? S + "second": "") + "" + dtstrs + '') ;}} (jQuery); time format jquery plugin

Usage

1. Reference The jquery plug-in above

2. Formatting time:

$.post("/tigger/GetUserPrizeLog?t=" + Math.random(), { activityCode: 1138, myOpenId: "@WorkContext.WxUserInfo.OpenId" }, function (json) {      var str = "";      $.each(json.Body, function (i, model) {        str += "
  • "; str += "

    " + model.ActivityPrizeName + "

    "; str += "

    " + $.Format(model.CreateTime, "yyyy-MM-dd hh:mm") + "

    "; str += ""; str += "
  • "; }); $("#record ul").html(str); });

    Model. CreateTime is the time obtained from the server.

    Usage:

    $. Format (model. CreateTime, "yyyy-MM-dd hh: mm ")

    The above in-depth understanding of the time js formatting in MVC is all the content shared by xiaobian. I hope to give you a reference, and I hope you can provide more support for the script house.

    Related Article

    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.