Date Processing js Library (mini version) -- self-built js library summary_time date-js tutorial

Source: Internet
Author: User
First, we recommend a good date js library on the Internet: momentjs.com. In fact, this type of resource network is a good one, but there are not many practical projects to be used, because the classes I come into contact with are often on the date rather than going deep into hour, minute, second! So I 'd like to compile a small library by myself, which will save a lot of effort in writing the purchase proxy later. Here I will share with you the hope to use the interface + inheritance + code optimization ideas for everyone.
First, I would like to share with you a very good js programming tips to achieve great code sharing! Because many js libraries perform direct prototype extension on native objects, this is a bad habit, which not only increases the memory consumption of each new instance object, and it is easy to cause pollution misunderstanding (think there is this thing )! This is also a criterion for building a js Library: as few prototype extensions as possible, especially the more rooted objects!

Js database creation principles
Js Library Standards (Dean Edwards in the Development of base2 some experience, including css3 and the latest browser API (such as querySelector) build-a-javascript-framework

Use inheritance to improve code sharing
Because it is not extended on the native object, an external namespace is required. In this object, some interfaces are provided for external calls. In order to improve the robustness of the js library, you need to minimize the number of external interfaces (the ideal is to save only the interfaces required by the user )! Here is a problem. Let me instantiate it:

The Code is as follows:


Var namespace = {
IOfirst: function (){},
IOsecond: function (){}
}


There is something in the object namespace that needs to be shared by IOfirst and IOsecond without exposing this interface! I encapsulate all external interfaces through a parent interface through inheritance, and then assign values in a uniform way under an init method. In the init method, due to the function of the closure, it achieves code sharing! Specific Practices:

Dynamically add external interfaces to enhance code flexibility

The Code is as follows:


AddIO: function (str ){
Var arrs = str. split ("."),
O = this;
For (I = (arrs [0] = "Date $ ")? 1: 0; i0)
{
Var data = arrs [0]
O [arrs [I] = o [arrs [I] | function () {return this. parIO. apply (null, [data, arguments])};
O = o [arrs [I];
}
}
InitFuns: function (){
Var that = this;
Var funs = this. actionFun;
// Init interface for all functions (test successfully)
Var interfaces = ["testLeap", "disDayNum", "todayBetween", "getMonthByDay", "getNextWeekByDay", "getWeekByDay", "newDate", "compareDate"]
Var shift;
Do {
Shift = interfaces. shift ()
That. addIO (shift );
Funs [shift] = function (){};
} While (interfaces. length> 0)
// Set the common object and variable

// For browers test
Var br = {
Ie: false, // IE6 ~ 8
Ff: false, // Firefox
Ch: false // Chrome
}
Var nav = navigator. userAgent;
If (! -[1,]) br. ie = true;
Else if (nav. indexOf ("Firefox")> 0) br. ff = true;
Else if (nav. indexOf ("Chrome")> 0) br. ch = true;

// Continue to set IO

}


Output the initialization completed interface on the console:
As a result, all external interfaces are bound to The parIO, so that a lot of code can be skipped when there are many interfaces! The key parIO that maintains internal and external hubs is responsible for finding sub-interfaces, passing parameters, and returning

The Code is as follows:


ParIO: function (){
If (Date $. actionFun [arguments [0])
{
Var customFun = Date $. actionFun [arguments [0]
Return customFun. apply (null, [arguments [1]);
}
Else
Console & cosnole. log ("empty ");
},


We can see that there are three parts:
In // set the common object and variable, we can write our public functions and variables. For example, to judge the browser, after adding sqlHelp functions similar to the background, we will initialize the interface:

The Code is as follows:


Funs. newDate = function (){
Return formatDate (arguments [0] [0])
}
Funs. getWeekByDay = function (){
Return getWeekByDay (arguments [0] [0]);
}
Funs. getNextWeekByDay = function (){
Var speDate = formatDate (arguments [0] [0]);
SpeDate. setDate (speDate. getDate () + 7 );
Return getWeekByDay (speDate );
}


Another benefit is that your code will not be copied and used by others, because the interface has a great internal connection! For example, the above funs. getWeekByDay and funs. getNextWeekByDay public the getWeekByDay () method! I have attached my immature js library and interface declaration, and hope Daniel can help me to improve it. Thank you very much.

The Code is as follows:


/*
// Function: to compare two dates and return information "equal"/"more"/"less"
// Arguments num: 2
// Arguments type: Date, Date
// Arguments declaration: 1.the target object you need to compare (Subtrahend); 2.the compare object (Minuend)
// Return data -- type: String; three value: "more" (if target is larger), "equal" (if target is equal to compared object ), "less" (if target is smaller)
CompareDate: function (objDate, comDate)
{
},
// Function: to format the string to Date, and return
// Arguments num: 1
// Arguments type: for this interface apply for overload, so String or Date is allowed
// Arguments declaration: if you pass String, it shocould format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY: MM: DD" like "2008/10/12"
// Return date: type: Date; one value: the date you want after formatting
NewDate: function (str)
{
},
// Function: get the start date and end date of the week of the date you have passed and return the Json including {startDay, endDay}
// Arguments num: 1
// Arguments type: for this interface apply for overload, so String or Date is allowed
// Arguments declaration: the day you want to get the first day and last day of in this weeek; if you pass String, it shoshould format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY: MM: DD" like "2008/10/12"
// Return data -- type: Json; one value: {startDay: "", endDay: ""}, you can get it by var. startDay (var is your custom variable)
GetWeekByDay: function (day)
{
},
// Function: get the start date and end date of next week of the date you have passed and return the Json including {startDay, endDay}
// Arguments num: 1
// Arguments type: for this interface apply for overload, so String or Date is allowed
// Arguments declaration: the day you want to get the first day and last day of in this weeek; if you pass String, it shoshould format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY: MM: DD" like "2008/10/12"
// Return data -- type: Json; one value: {startDay: "", endDay: ""}, you can get it by var. startDay (var is your custom variable)
GetNextWeekByDay: function (day)
{
};
// Function: get the start date and end date of the month of the date you have passed and return the Json including {startDay, endDay}
// Arguments num: 1
// Arguments type: Date
// Arguments declaration: the day you want to get the first day and last day of in this month
// Return data -- type: Json; one value: {startDay: "", endDay: ""}, you can get it by var. startDay (var is your custom variable)
GetMonthByDay: function (day)
{
},
// Function: to test if including today between the two dates you pass and return in boolean
// Arguments num: 2
// Arguments type: Date, Date
// Arguments declaration: the procedure will test the larger and sort automatically, so the order is no matter
// Return data -- type: boolean; one value: true if today is between the two dates
TodayBetween: function (startDate, endDate)
{
},
// Function: to caculate the difference between two dates in days
// Arguments num: 2
// Arguments type: Date, Date
// Arguments declaration: 1. startDate (the one be supported CED) 2. endDate (the one to reduce)
// Return data -- type: Number; one value: the different between these two dates
DisDayNum: function (startDate, endDate)
{
},
// Function: test the year of the date you have passed leap or not and return in boolean
// Arguments num: 1
// Arguments type: for this interface apply for overload, so String or Date is allowed
// Arguments declaration: if you pass String, it shocould format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY: MM: DD" like "2008/10/12"
// Return data -- type: boolean; one value: true if it is leap year or false
TestLeap: function (date)
{
}*/


Welcome to download: Date $. js

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.