An excellent program must take into account both I18n and L10N, but Javascript is very unfriendly in Date processing. The problem is reflected in the following aspects:
Although Js also sets a UTC time function, the time zone when the script is running is automatically Obtained from the system and cannot be changed. In practical applications, it is often necessary to accept time data that is not in the current system time zone for conversion. At this time, Js's cleverness brings a lot of trouble.
JS's automatic parsing and formatted output of dates perform differently depending on the system environment and browser environment. This is mainly reflected in the Date.parse and toLocaleString methods. Interested students can test it by themselves.
In order to improve the Date processing capabilities of Js as easily as possible, only two extensions are made to the Date object of Js
/**
* Get the UTC timestamp of the current time
* @return {int} unix timestamp
*/
Date.prototype.getTimeUTC = function() {
return this.getTime() + this.getTimezoneOffset() * 60 * 1000;
}
/**
* Change the time of the current operation to the time zone (mainly used to convert the time in another time zone)
*
* @param {int} tzo original time zone-12~13
* @param {int} tzo target time zone-12~13 defaults to the current time zone
*/
Date.prototype.changeTimezone = function(tzo,tzn) {
tzo = tzo * 60;
tzn = tzn ? tzn * 60 : -this.getTimezoneOffset();
this.setTime(this.getTime() - (tzo - tzn) * 60 * 1000);
}
/**
* Get the UTC timestamp of the current time
* @return {int} unix timestamp
*/
Date.prototype.getTimeUTC = function() {
return this.getTime() + this.getTimezoneOffset() * 60 * 1000;
}
/**
* Change the time of the current operation to the time zone (mainly used to convert the time in another time zone)
*
* @param {int} tzo original time zone-12~13
* @param {int} tzo target time zone-12~13 defaults to the current time zone
*/
Date.prototype.changeTimezone = function(tzo,tzn) {
tzo = tzo * 60;
tzn = tzn ? tzn * 60 : -this.getTimezoneOffset();
this.setTime(this.getTime() - (tzo - tzn) * 60 * 1000);
}
At this point, new development can be carried out based on this extended Js Date object.
The idea is very simple, that is, first transfer the time in any format and time zone to the same time zone as the current system through the changeTimezone method, and then operate on the UTC Unix timestamp generated by getTimeUTC.
For example, I want to calculate the time difference between 4 Jun 2008, 16:30 in the +8 time zone and the current time in the +9 time zone
// automatically parse it into a unix timestamp
var p = Date.parse('4 Jun 2008, 16:30');
var time_parse = new Date(p);
//Convert to the time zone to be compared
time_parse.changeTimezone(8,9);
var time_now = new Date();
//Convert to UTC for comparison
var der = time_now.getTimeUTC() - time_parse.getTimeUTC();
alert('difference' + parseInt(der / 1000 / 60) + 'minutes');
//Automatically parse into unix timestamp
var p = Date.parse('4 Jun 2008, 16:30');
var time_parse = new Date(p);
//Convert to the time zone to be compared
time_parse.changeTimezone(8,9);
var time_now = new Date();
//Convert to UTC for comparison
var der = time_now.getTimeUTC() - time_parse.getTimeUTC();
alert('difference' + parseInt(der / 1000 / 60) + 'minutes');
Of course there are simpler codes, but it is less error-prone to clarify ideas in complex applications.
If you want to implement a more user-friendly reminder like XX days XX months ago in the left column of this blog, you can further expand it here according to your needs. The implemented functions are as follows
/**
* represents the difference between the specified time and now
*
* @param {int} t The time to be compared unix timestamp (UTC)
* @param {int} n as the standard time, defaults to the current time unix timestamp (UTC)
* @return {string} expression of the difference time
*/
Date.prototype.derTime = function(t,n) {
var n = n ? n : this.getTimeUTC();
function ms2min(ms) {
return parseInt(ms / 1000 / 60);
}
var der = ms2min(n - t);
var ba = der > 0 ? 'before' : 'after';
der = Math.abs(der);
var res = '';
if(der == 0) {
res = 'just';
}
else if(0 < der && der < 60) {
res = der + 'minute' + ba;
}
else if(60 <= der && der < 24 * 60) {
var min = der % 60 == 0 ? '' : String(der % 60) + 'minutes';
res = String(parseInt(der / 60)) + 'hour' + min + ba;
}
else if(der >= 24 * 60 && der < 24 * 60 * 31) {
res = String(parseInt(der / 60 / 24)) + 'day' + ba;
}
else if(der >= 24 * 60 * 31 && der < 24* 60 * 365) {
res = String(parseInt(der / 60 / 24 / 31)) + 'month' + ba;
}
else if(der > 24 * 60 * 365) {
res = String(parseInt(der / 60 / 24 / 365)) + 'year' + ba;
}
return res;
}
/**
* Parse the difference between a time field and the current time
* @param {string} i
* @param {int} time zone-12~13
*/
function time_der(i,tz) {
var p = Date.parse(i);
if(!p) {
return i;
}
var time_parse = new Date(p);
if(tz != undefined) {
time_parse.changeTimezone(tz);
}
var time_now = new Date();
return time_now.derTime(time_parse.getTimeUTC());