Both front-end and background-to-value operations for time values (JavaScript and C # two methods)

Source: Internet
Author: User
Tags string format

Recently in a video playback project, record the next summary.

Application background:

Suppose there is an access control system that records the access card information, access to video recording equipment, and now according to the person's credit card time to recall the time before and after a period of video. About the video playback part of the specific implementation process is more complex this article does not involve, mention the time value of the increase and decrease of the operation.

Goal:

For example, the string "2015-01-01 00:00:00" in the time format, returns a string that is the time format of 30 seconds and 30 seconds before that point in time.

Both front-end and background start-up can be implemented, but only in a different way.

Background (C #) Implementation method:

1   stringstr ="2015-01-01 00:00:00";2DateTime dt = convert.todatetime (str);//Convert to DateTime format, and then call its related methods3 4   stringstime = dt. AddSeconds (- -). ToString ("YYYY-MM-DD HH:mm:ss");//First 30 seconds5   stringetime = dt. AddSeconds ( -). ToString ("YYYY-MM-DD HH:mm:ss");//after 30 seconds ( reminder : The AddSeconds () method of DateTime in C # only returns processing results without changing the original value of the DateTime object, so the DT here is still "2015-01-01 00:00:00", This is different from the following JavaScript methods)6Console.WriteLine (stime);//output: "2014-12-31 23:59:30"7Console.WriteLine (etime);//output: "2015-01-01 00:00:30"8 

Output Result:

Front-end JavaScript implementation methods:

1   //Refer to the CSDN great God Meizz A format method added to the Date object2Date.prototype.Format =function (FMT)3{//Author:meizz4       varn \ {   5       "m+": This. GetMonth () +1,//Month6       "d+": This. GetDate (),//Day7       "H +": This. GetHours (),//hours8       "m+": This. getminutes (),//points9       "s+": This. getseconds (),//secondsTen       "q+": Math.floor (( This. GetMonth () +3)/3),//Quarterly One       "S": This. Getmilliseconds ()//milliseconds A   };  -   if(/(y+)/. Test (FMT)) -Fmt=fmt.replace (regexp.$1, ( This. getFullYear () +""). substr (4-regexp.$1. length));  the    for(varKincho) -       if(NewREGEXP ("("+ K +")"). Test (FMT)) -FMT = Fmt.replace (regexp.$1, (regexp.$1. length==1) ? (O[k]): (("xx"+ O[k]). substr (""+o[k]));  -     returnFMT;  +   } -   //The following is a concrete example +   varStr='2015-01-01 00:00:00'; AStr=str.replace (/-/g,'/'); //Convert to "2015/01/01 00:00:00"  at   varstrt=NewDate (str); //Convert to date object     -              - varStrint= Strt.gettime ();//returns the number of milliseconds since January 1, 1970 (Reminder: If you dig a bit, here is the standard Greenwich mean, in China can be verified, 1970-01-01 00:00:00 the number of milliseconds returned is 28800000, that is-8 hours, This is because China is the 8th time zone, so it is 8 hours later than the 0 time zone . -              -   vart1=strint-30000; //minus 30 seconds (note unit is milliseconds, so it is 30000)  -   vart2=strint+30000; //Plus 30 seconds  in              -   varObjdate=NewDate (); //Declares a Date object, in order to use the object's SetTime () method, at which point the Objdate object has no specific value
to + objdate.settime (t1); //Based on the number of milliseconds converted to a specific time, this will return the GMT format "Wed Dec 23:59:30 GMT +0800 (Chinese Time)" (reminder: Each call to the SetTime () method of a Date object changes the value of the object itself. Now the objdate is not empty, which is different from the Datetime.addseconds () method in C # above) - varStime=objdate.format ("YYYY-MM-DD Hh:mm:ss"); //Call the format () method of the Date object above Meizz to convert to the standard string format "2014-12-31 23:59:30" the objdate.settime (T2); //Similar, return GMT format * varEtime=objdate.format ('YYYY-MM-DD Hh:mm:ss'); //Convert to standard string format $ Panax NotoginsengAlert'stime Type:'+typeof(stime) +', the value is:'+stime+'\netime Type:'+typeof(etime) +', the value is:'+etime);

Output Result:

Summary: He mountain stone, can attack Jade. This record is only the operation of the second, the other of the month and day of operation is similar to this.

Both front-end and background-to-value operations for time values (JavaScript and C # two methods)

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.