JavaScript Notes Basics (iii)

Source: Internet
Author: User

To get the current time or a summary of the time data processing method for the front section JS:

JavaScript string converted to date

Java Code   

    1. var s= "2010-5-18 12:30:20";
    2. var t=New Date (S.replace (/-/g, "/"));



Java code

Java Code   

    1. <%
    2. SimpleDateFormat DateFormat = new simpledateformat ("Yyyy-mm-dd");
    3. Date acceptdate = Dateformat.parse (acceptdate);
    4. long diffmillis=acceptdate.gettime ()-salvation.getarrivedate (). GetTime ();
    5. long arrive = diffmillis/(24*60*60*1000);
    6. String arriveday=null;
    7. %>



JavaScript code

Java Code   

  1. <script type= "Text/javascript" >
  2. function Datechange ()
  3. {
  4. var Starttime=document.getelementbyid ("Indatenull"). Value;
  5. var Stoptime=document.getelementbyid ("Acceptdatenull"). Value;
  6. var D1 = new Date (Starttime.replace (/-/g, "/"));
  7. var d2 = new Date (Stoptime.replace (/-/g, "/"));
  8. var times = D2.gettime ()-d1.gettime ();
  9. var days = times/(24*60*60*1000);
  10. document.getElementById ("B1"). Value=days;
  11. 11.}
  12. </script>

gets a collection of methods for the current time , various Types

JS (JQuery) method of acquiring time and common time class

From: http://blog.csdn.net/liujun198773/article/details/7554628 thanks

$ (function () {

var mydate = new Date ();
var t=mydate.tolocalestring ();
/*alert (t); */
$ ("#time"). Text (t);
$ ("#time"). Load ("untitled-1.html");

});

<pid= "Time" >afwef</P>

Time to get JavaScript is done using the built-in date function

Varmydate = new Date ();
Mydate.getyear (); Get Current year (2-bit)
Mydate.getfullyear (); Get the full year (4-bit, 1970-????)
Mydate.getmonth (); Get the current month (0-11, 0 for January)
Mydate.getdate (); Get current day (1-31)
Mydate.getday (); Get Current week x (0-6, 0 for Sunday)
Mydate.gettime (); Gets the current time (the number of milliseconds since 1970.1.1)
Mydate.gethours (); Get current number of hours (0-23)
Mydate.getminutes (); Gets the current number of minutes (0-59)
Mydate.getseconds (); Gets the current number of seconds (0-59)
Mydate.getmilliseconds (); Gets the current number of milliseconds (0-999)
Mydate.tolocaledatestring (); Get Current date
var mytime=mydate.tolocaletimestring (); Get current time
Mydate.tolocalestring (); Get Date and time


Date Time Script Library method list

Date.prototype.isLeapYear Judging leap years
Date.prototype.Format date Formatting
DATE.PROTOTYPE.DATEADD Date Calculation
Date.prototype.DateDiff Comparison Date Difference
Date.prototype.toString Date to String
Date.prototype.toArray date split into arrays
Date.prototype.DatePart Partial information of the date taken
Date.prototype.MaxDayOfDate the maximum number of days in the month in which the date is taken
Date.prototype.WeekNumOfYear the week of the year in which the date is judged
Stringtodate String to date type
Isvaliddate Verifying date Validity
Checkdatetime Full date and time check
Daysbetween Date Days Difference


//---------------------------------------------------
Judging leap years
//---------------------------------------------------

Date.prototype.isLeapYear = function ()
{
Return (0==this.getyear ()%4&& ((This.getyear () 0!=0) | | (This.getyear () @0==0));
}

//---------------------------------------------------
Date formatting
Format Yyyy/yyyy/yy/yy represents the year
Mm/m Month
W/W Week
dd/dd/d/d Date
hh/hh/h/h time
mm/m min
SS/SS/S/S sec
//---------------------------------------------------
Date.prototype.Format = function (FORMATSTR)
{
var str = FORMATSTR;
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];

Str=str.replace (/yyyy| Yyyy/,this.getfullyear ());
Str=str.replace (/yy| yy/, (this.getyear ()%) >9? (This.getyear ()%100). ToString (): ' 0 ' + (this.getyear ()% 100));

Str=str.replace (/mm/,this.getmonth () >9?this.getmonth (). toString (): ' 0 ' +this.getmonth ());
Str=str.replace (/m/g,this.getmonth ());

Str=str.replace (/w| W/g,week[this.getday ()]);

Str=str.replace (/dd| Dd/,this.getdate () >9?this.getdate (). toString (): ' 0 ' +this.getdate ());
Str=str.replace (/d| D/g,this.getdate ());

Str=str.replace (/hh| Hh/,this.gethours () >9?this.gethours (). toString (): ' 0 ' +this.gethours ());
Str=str.replace (/h| H/g,this.gethours ());
Str=str.replace (/mm/,this.getminutes () >9?this.getminutes (). toString (): ' 0 ' +this.getminutes ());
Str=str.replace (/m/g,this.getminutes ());

Str=str.replace (/ss| Ss/,this.getseconds () >9?this.getseconds (). toString (): ' 0 ' + this.getseconds ());
Str=str.replace (/s| S/g,this.getseconds ());

return str;
}

//+---------------------------------------------------
//| The number of days for two time difference date format is YYYY-MM-DD
//+---------------------------------------------------
function Daysbetween (dateone,datetwo)
{
var onemonth = dateone.substring (5,dateone.lastindexof ('-'));
var oneday = dateone.substring (Dateone.length,dateone.lastindexof ('-') +1);
var oneyear = dateone.substring (0,dateone.indexof ('-'));

var twomonth = datetwo.substring (5,datetwo.lastindexof ('-'));
var twoday = datetwo.substring (Datetwo.length,datetwo.lastindexof ('-') +1);
var twoyear = datetwo.substring (0,datetwo.indexof ('-'));

var cha= ((Date.parse (onemonth+ '/' +oneday+ '/' +oneyear)-date.parse (twomonth+ '/' +twoday+ '/' +twoyear))/86400000);
Return Math.Abs (CHA);
}


//+---------------------------------------------------
//| Date Calculation
//+---------------------------------------------------
DATE.PROTOTYPE.DATEADD = function (strinterval, number) {
var dttmp = this;
Switch (strinterval) {
Case ' s ': return new Date (Date.parse (dttmp) + (* number));
Case ' n ': return new Date (Date.parse (dttmp) + (60000 * number));
Case ' H ': return new Date (Date.parse (dttmp) + (3600000 * number));
Case ' d ': return new Date (Date.parse (dttmp) + (86400000 * number));
Case ' W ': Return new Date (Date.parse (dttmp) + ((86400000 * 7) * number));
Case ' Q ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number*3,dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
Case ' m ': return new Date (Dttmp.getfullyear (), (Dttmp.getmonth ()) + number,dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
Case ' Y ': Return new Date ((dttmp.getfullyear () + number), Dttmp.getmonth (), Dttmp.getdate (), dttmp.gethours (), Dttmp.getminutes (), dttmp.getseconds ());
}
}

//+---------------------------------------------------
//| Comparison Date Difference dtend format is a date type or a valid date format string
//+---------------------------------------------------
Date.prototype.DateDiff = function (Strinterval, dtend) {
var dtstart = this;
if (typeof dtend = = ' string ')//if the string is converted to a date type
{
Dtend = Stringtodate (dtend);
}
Switch (strinterval) {
Case ' s ': Return parseint ((Dtend-dtstart)/1000);
Case ' n ': Return parseint ((Dtend-dtstart)/60000);
Case ' H ': Return parseint ((dtend-dtstart)/3600000);
Case ' d ': Return parseint ((dtend-dtstart)/86400000);
Case ' W ': Return parseint ((Dtend-dtstart)/(86400000 * 7));
Case ' m ': Return (Dtend.getmonth () +1) + ((Dtend.getfullyear ()-dtstart.getfullyear ()) *12)-(Dtstart.getmonth () +1);
Case ' Y ': return dtend.getfullyear ()-dtstart.getfullyear ();
}
}

//+---------------------------------------------------
//| Date output string, overloading the system's ToString method
//+---------------------------------------------------
Date.prototype.toString = function (Showweek)
{
var mydate= this;
var str = mydate.tolocaledatestring ();
if (Showweek)
{
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
str + = ' Week ' + week[mydate.getday ()];
}
return str;
}

//+---------------------------------------------------
//| Validation of date legitimacy
//| Format: YYYY-MM-DD or YYYY/MM/DD
//+---------------------------------------------------
function Isvaliddate (DATESTR)
{
var sdate=datestr.replace (/(^\s+|\s+$)/g, '); Go on both sides of the space;
if (sdate== ") return true;
If the format satisfies yyyy-(/) mm-(/) DD or yyyy-(/) m (/) DD or yyyy-(/) m (/) d or yyyy-(/) mm-(/) d is replaced by "
In the database, the legal date can be: Yyyy-mm/dd (2003-3/21), the database is automatically converted to the YYYY-MM-DD format
var s = sdate.replace (/[\d]{}[\-/]{1}[\d]{}[\-/]{1}[\d]{1,2}/g, ');
if (s== ')//description format satisfies YYYY-MM-DD or YYYY-M-DD or yyyy-m-d or yyyy-mm-d
{
var t=new Date (Sdate.replace (/\-/g, '/'));
var ar = sdate.split (/[-/:]/);
if (ar[0]! = t.getyear () | | ar[1]! = T.getmonth () +1 | | ar[2]! = T.getdate ())
{
Alert (' Wrong date format! The format is: Yyyy-mm-dd or YYYY/MM/DD. Note leap years. ‘);
return false;
}
}
Else
{
Alert (' Wrong date format! The format is: Yyyy-mm-dd or YYYY/MM/DD. Note leap years. ‘);
return false;
}
return true;
}

//+---------------------------------------------------
//| Date Time Check
//| Format is: YYYY-MM-DDHH:MM:SS
//+---------------------------------------------------
function Checkdatetime (str)
{
var reg =/^ (\d+)-(\d{)-(\d{) (\d{)):(\d{):(\d{) $/;
var r = Str.match (reg);
if (r==null) return false;
R[2]=r[2]-1;
var d= new Date (R[1],r[2],r[3],r[4],r[5],r[6]);
if (D.getfullyear ()!=r[1]) return false;
if (D.getmonth ()!=r[2]) return false;
if (D.getdate ()!=r[3]) return false;
if (D.gethours ()!=r[4]) return false;
if (D.getminutes ()!=r[5]) return false;
if (D.getseconds ()!=r[6]) return false;
return true;
}

//+---------------------------------------------------
//| Divide a date into an array
//+---------------------------------------------------
Date.prototype.toArray = function ()
{
var mydate = this;
var MyArray = Array ();
Myarray[0] = Mydate.getfullyear ();
MYARRAY[1] = Mydate.getmonth ();
MYARRAY[2] = Mydate.getdate ();
MYARRAY[3] = mydate.gethours ();
MYARRAY[4] = Mydate.getminutes ();
MYARRAY[5] = Mydate.getseconds ();
return myArray;
}

//+---------------------------------------------------
//| Get Date data information
//| Parameter interval represents the data type
//| Y year M D Day W week WW week h N min S S
//+---------------------------------------------------
Date.prototype.DatePart = function (interval)
{
var mydate = this;
var partstr= ';
var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six '];
Switch (interval)
{
Case ' y ':p artstr = Mydate.getfullyear ();
Case ' m ':p artstr = Mydate.getmonth () +1;break;
Case ' d ':p artstr = Mydate.getdate ();
Case ' W ':p artstr = Week[mydate.getday ()];break;
Case ' ww ':p artstr = Mydate.weeknumofyear ();
Case ' h ':p artstr = mydate.gethours ();
Case ' n ':p artstr = Mydate.getminutes ();
Case ' s ':p artstr = Mydate.getseconds ();
}
return partstr;
}

//+---------------------------------------------------
//| Maximum number of days to get the current date in the month
//+---------------------------------------------------
Date.prototype.MaxDayOfDate = function ()
{
var mydate = this;
var ary = Mydate.toarray ();
var date1 = (new Date (ary[0],ary[1]+1,1));
var date2 = date1.dateadd (1, ' m ', 1);
var result = DateDiff (date1. Format (' Yyyy-mm-dd '), Date2. Format (' yyyy-mm-dd '));
return result;
}

//+---------------------------------------------------
//| The week in which the current date is obtained is the week of the year
//+---------------------------------------------------
Date.prototype.WeekNumOfYear = function ()
{
var mydate = this;
var ary = Mydate.toarray ();
var year = ary[0];
var month = ary[1]+1;
var day = ary[2];
document.write (' < script language=vbscript\> \ n ');
document.write (' mydate = DateValue (' +month+ '-' +day+ '-' +year+ ') \ n ');
document.write (' result = DatePart (' ww ', mydate) \ n ');
document.write (' \ n ');
return result;
}

//+---------------------------------------------------
//| The string is converted to a date type
//| Format mm/dd/yyyy mm-dd-yyyy yyyy/mm/dd yyyy-mm-dd
//+---------------------------------------------------
function Stringtodate (DATESTR)
{

var converted = Date.parse (DATESTR);
var mydate = new Date (converted);
if (IsNaN (mydate))
{
var Delimcahar = datestr.indexof ('/')!=-1? ' /‘:‘-‘;
var arys= datestr.split ('-');
MyDate = new Date (Arys[0],--arys[1],arys[2]);
}
return mydate;
}

Create a pull-down menu linkage state

cannot be used JQuery only native selectors can be used

functioncreatelist (selecteditem,gameinfo,ddtext,type,data) {varoption=$ ("<option></option>", {"Data_selecteditem": SelectedItem, "Data_gameinfo": Gameinfo, "Data_ddtext": Ddtext, "Data_type": type});            Option.text (SelectedItem); $("#" +data). On ("Change",function() {changeproduct (data);            }); $("#"+data). Append (option); } functionchangeproduct (data) {Varitem=document.getElementById (data). Options[document.getelementbyid (data). SelectedIndex]; if(item) {Varselecteditem=item.attributes["Data_selecteditem"].value, Gameinfo=item.attributes["Data_gameinfo"].value, Ddtext=item.attributes["Data_ddtext"].value, type=item.attributes["Data_type"].value;            SelectItem (Selecteditem,gameinfo,ddtext,type); }            return; }

JavaScript Notes Basics (iii)

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.