Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn the time processing class library. You can pass in the timestamp. One time written by myself, such as one minute before mobile phone communication, two hours ago, yesterday, three days ago, January 1, April 5
One time written by myself, such as one minute before mobile phone communication, two hours ago, yesterday, three days ago, January 1, April 5
Timestamp can be input
Demo http://mall.hnsanx.com/yzzs/dataclass/date.html
Key code:
Var NOW = new Date (); // obtain the current system time
Date. prototype. format = function (format ){
Var o = {
"M +": this. getMonth () + 1, // month
"D +": this. getDate (), // day
"H +": this. getHours (), // hour
"M +": this. getMinutes (), // minute
"S +": this. getSeconds (), // second
"Q +": Math. floor (this. getMonth () + 3)/3), // quarter
"S": this. getMilliseconds () // millisecond
}
If (/(y +)/. test (format )){
Format = format. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length ));
}
For (var k in o ){
If (new RegExp ("(" + k + ")"). test (format )){
Format = format. replace (RegExp. $1, RegExp. $1. length = 1? O [k]: ("00" + o [k]). substr ("" + o [k]). length ));
}
}
Return format;
}
Var dateClass = {
// ======================================
// The conversion timestamp.
// DateClass. transdate ("14:16:32 ");
// 2015-03-30
// ======================================
Transdate: function (endTime ){
Var date = new Date ();
Date. setFullYear (endTime. substring (0, 4 ));
Date. setMonth (endTime. substring (5, 7)-1 );
Date. setDate (endTime. substring (8, 10 ));
Date. setHours (endTime. substring (11,13 ));
Date. setMinutes (endTime. substring (14,16 ));
Date. setSeconds (endTime. substring (17,19 ));
Return Date. parse (date)/1000;
},
// ======================================
// Time formatted output to get the current time
// DateClass. formatDate (1230999938, 'yyyy-MM-dd hh: mm: ss'); 1230999938 is the timestamp
// 2015-03-30
// ======================================
FormatDate: function (strTime, format ){
Var format = format | 'yyyy-MM-dd hh: mm: ss ';
Var d = strTime? New Date (strTime): new Date ();
Return d. format (format );
},
// ======================================
// Format the crawler time
// DateClass. formatDate ("YYYY-MM-DD H: I: s ");
// 2015-03-30
// ======================================
DateProcess: function (oldDate ){
// Alert (isNaN (oldDate ));
If (! IsNaN (oldDate) oDateSeconds = oldDate;
Else {
Var oDate = this. formatDate (oldDate) // Time Standard Format
Var oDateSeconds = this. transdate (oDate );
}
// Var oDate = this. formatDate (oldDate)
Var nowStr = NOW. format ('yyyy-MM-dd hh: mm: ss'); // format the current system time.
Var nowSeconds = this. transdate (nowStr );
// Alert (nowSeconds + "and" + oDateSeconds );
If (nowSeconds> oDateSeconds ){
Var ToSeconds = nowSeconds-oDateSeconds;
If (ToSeconds <(86400*365 )){
If (ToSeconds <(86400*7 )){
If (ToSeconds <(86400*2 )){
If (ToSeconds <86400 ){
If (ToSeconds <3600 ){
If (ToSeconds <60 ){
Return ToSeconds + "seconds ago"; // 59 seconds ago
} Else {
Return Math. floor (ToSeconds/60) + "Minutes Ago"; // 59 minutes ago
}
} Else {
Return Math. floor (ToSeconds/3600) + "Hours Ago"; // 23 hours ago
}
} Else {
Var nowHour = NOW. getHours ();
If (ToSeconds> (nowHour + 24) * 3600) return "1 day ago ";
Else return "yesterday"; // Yesterday
}
} Else {
Return Math. floor (ToSeconds/86400) + "Days Ago"; // 6 days ago
}
}
Else {
Return (new Date (oldDate). format ("M-month-day"); // output on January 1, December 12
}
} Else {
// Alert (ToSeconds );
Return (new Date (oldDate). format ("yyyy-MM-dd") // output YYYY-MM-dd
}
} Else {
Return "time ahead"; // The time to be processed is the past time.
}
}
}
DateClass.rar (1.28 KB download: 21 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB