Time Processing, space dynamics 1 minute ago, yesterday, 2 days ago

Source: Internet
Author: User
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

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.