Skip to content
Snippets Groups Projects
Commit 1ee3f721 authored by Anduin Xue's avatar Anduin Xue
Browse files

Upgrade to 2.0.0

parent f967437d
No related branches found
No related tags found
No related merge requests found
LICENSE 0 → 100644
MIT License
Copyright (c) 2017 Anduin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
......@@ -16,7 +16,13 @@ And add your reference:
```html
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/jquery-utc-time/src/jquery-utc-time.js"></script>
<script src="node_modules/jquery-utc-time/dist/jquery-utc-time.min.js"></script>
```
Or using es6:
```javascript
import 'jquery-utc-time';
```
### Use via CDN
......@@ -49,7 +55,7 @@ Add init jquery-utc-time
```html
<script>
$(this).initUTCTime({ });
new UtcTime({ });
</script>
```
......@@ -61,7 +67,7 @@ And when it starts, it shows like this.
```javascript
// Init with loop
$(this).initUTCTime({
new UtcTime({
// We will try to select elements using the attr value. Default value is 'data-utc-time'.
attr: 'data-utc-time',
......@@ -84,11 +90,9 @@ And when it starts, it shows like this.
disableAgo: false,
// Always disable the hover effect.
disableHover: false
});
```
disableHover: false,
```javascript
// Tripgger force update.
$(this).initUTCTimeOnce();
// Disable auto update the value by seconds.
disableAutoUpdate: false
});
```
!function (f) { function e(h) { h.attr || (h.attr = "data-utc-time"), f("*[" + h.attr + "]").each(function (t) { var o = f(this), e = o.attr(h.attr), a = e; e.endsWith(" UTC") || (a = e + " UTC"); var r = new Date(a); if (isNaN(r.getTime())) { a = e.endsWith("Z") ? e : e + "Z"; r = new Date(a) } var i, n, s, g, l = (i = r, n = h, s = Math.floor((new Date - i) / 1e3), 1 < (g = Math.floor(s / 2592e3)) ? n.format ? i.Format(n.format) : i.toLocaleDateString() : 1 < (g = Math.floor(s / 86400)) ? n.daysAgo ? g + n.daysAgo : g + " days ago" : 1 < (g = Math.floor(s / 3600)) ? n.hoursAgo ? g + n.hoursAgo : g + " hours ago" : 1 < (g = Math.floor(s / 60)) ? n.minutesAgo ? g + n.minutesAgo : g + " minutes ago" : 0 <= g ? n.secondsAgo ? Math.floor(s) + n.secondsAgo : Math.floor(s) + " seconds ago" : n.format ? i.Format(n.format) : i.toLocaleDateString()); h.disableAgo && (l = h.format ? r.Format(h.format) : r.toLocaleDateString()), o.html(l), o.tooltip && !h.disableHover && (o.attr("data-toggle", "tooltip"), o.attr("data-trigger", "hover"), o.attr("data-title", r.toLocaleString()), o.tooltip()) }) } Date.prototype.Format = function (t) { var o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), S: this.getMilliseconds() }; for (var e in /(y+)/.test(t) && (t = t.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))), o) new RegExp("(" + e + ")").test(t) && (t = t.replace(RegExp.$1, 1 == RegExp.$1.length ? o[e] : ("00" + o[e]).substr(("" + o[e]).length))); return t }, f.fn.initUTCTime = function (t) { null != t && null != t || (t = {}); var o = function () { e(t), setTimeout(o, 1e3) }; o() } }(jQuery);
\ No newline at end of file
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("jQuery"));else if("function"==typeof define&&define.amd)define(["jQuery"],e);else{var o="object"==typeof exports?e(require("jQuery")):e(t.jQuery);for(var r in o)("object"==typeof exports?exports:t)[r]=o[r]}}(this,(function(t){return function(t){var e={};function o(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,o),n.l=!0,n.exports}return o.m=t,o.c=e,o.d=function(t,e,r){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(r,n,function(e){return t[e]}.bind(null,n));return r},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o(o.s=1)}([function(e,o){e.exports=t},function(t,e,o){"use strict";o.r(e),o.d(e,"UtcTime",(function(){return i}));var r=o(0),n=o.n(r);class i{constructor(t){let e={attr:"data-utc-time",format:"",daysAgo:" days ago",hoursAgo:" hours ago",minutesAgo:" minutes ago",secondsAgo:" seconds ago",disableAgo:!1,disableHover:!1,disableAutoUpdate:!1};Object.assign(e,t),this.initFormat();var o=this,r=function(){o.initTime(e,o),e.disableAutoUpdate||setTimeout(r,1e3)};r()}timeSince(t,e){var o=Math.floor((new Date-t)/1e3),r=Math.floor(o/2592e3);return r>1?e.format?t.Format(e.format):t.toLocaleDateString():(r=Math.floor(o/86400))>1?r+e.daysAgo:(r=Math.floor(o/3600))>1?r+e.hoursAgo:(r=Math.floor(o/60))>1?r+e.minutesAgo:r>=0?Math.floor(o)+e.secondsAgo:e.format?t.Format(e.format):t.toLocaleDateString()}initTime(t,e){n()("*["+t.attr+"]").each((function(o){var r=n()(this),i=r.attr(t.attr),a=i;i.endsWith(" UTC")||(a=i+" UTC");var s=new Date(a);if(isNaN(s.getTime())){a=i.endsWith("Z")?i:i+"Z";s=new Date(a)}var u=e.timeSince(s,t);t.disableAgo&&(u=t.format?s.Format(t.format):s.toLocaleDateString()),r.html(u),r.tooltip&&!t.disableHover&&(r.attr("data-toggle","tooltip"),r.attr("data-trigger","hover"),r.attr("data-title",s.toLocaleString()),r.tooltip())}))}initFormat(){Date.prototype.Format=function(t){var e={"M+":this.getMonth()+1,"d+":this.getDate(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};for(var o in/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))),e)new RegExp("("+o+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?e[o]:("00"+e[o]).substr((""+e[o]).length)));return t}}}}])}));
\ No newline at end of file
<p data-utc="2019/1/25 2:34:11"></p>
<p data-utc="2019-08-05T10:42:29.2"></p>
<p data-utc="2020/5/15 2:34:11"></p>
<p data-utc="2020-05-17T06:05:29.2"></p>
<script src="node_modules/jquery/dist/jquery.js"></script>
<script src="src/jquery-utc-time.js"></script>
<script src="dist/jquery-utc-time.min.js"></script>
<script>
$(this).initUTCTime({
new UtcTime({
attr: 'data-utc',
format: 'yyyy年 MM月 dd日 hh:mm:ss',
daysAgo: '天前',
hoursAgo: '小时前',
minutesAgo: '分钟前',
secondsAgo: '秒前'
//disableAgo: false
//disableHover: false
});
</script>
This diff is collapsed.
{
"name": "jquery-utc-time",
"version": "1.3.11",
"version": "2.0.0",
"description": "A jquery plugin which converts UTC time to local time easily.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "webpack"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/Anduin2017/jquery-utc-time.git"
},
"keywords": [
"jquery"
],
"main": "dist/jquery-utc-time.min.js",
"files": [
"dist/jquery-utc-time.min.js"
],
"author": "anduin@aiursoft.com",
"license": "ISC",
"bugs": {
"url": "https://github.com/Anduin2017/jquery-utc-time/issues"
},
"license": "MIT",
"homepage": "https://github.com/Anduin2017/jquery-utc-time#readme",
"dependencies": {
"jquery": "^3.4.0"
"jquery": "^3.5.1"
},
"devDependencies": {
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
}
}
(function ($) {
import $ from 'jquery';
var timeSince = function (date, settings) {
class UtcTime {
constructor(settings) {
let defaultSettings = {
attr: 'data-utc-time',
format: '',
daysAgo: ' days ago',
hoursAgo: ' hours ago',
minutesAgo: ' minutes ago',
secondsAgo: ' seconds ago',
disableAgo: false,
disableHover: false,
disableAutoUpdate: false
};
Object.assign(defaultSettings, settings);
this.initFormat();
var self = this;
var loop = function () {
self.initTime(defaultSettings, self);
if (!defaultSettings.disableAutoUpdate) {
setTimeout(loop, 1000);
}
};
loop();
}
timeSince(date, settings) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 2592000);
if (interval > 1) {
......@@ -11,30 +37,18 @@
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
if (settings.daysAgo) {
return interval + settings.daysAgo;
}
return interval + " days ago";
return interval + settings.daysAgo;
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
if (settings.hoursAgo) {
return interval + settings.hoursAgo;
}
return interval + " hours ago";
return interval + settings.hoursAgo;
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
if (settings.minutesAgo) {
return interval + settings.minutesAgo;
}
return interval + " minutes ago";
return interval + settings.minutesAgo;
}
if (interval >= 0) {
if (settings.secondsAgo) {
return Math.floor(seconds) + settings.secondsAgo;
}
return Math.floor(seconds) + " seconds ago";
return Math.floor(seconds) + settings.secondsAgo;
} else {
if (settings.format) {
return date.Format(settings.format);
......@@ -43,10 +57,7 @@
}
};
var initTime = function (settings) {
if (!settings.attr) {
settings.attr = 'data-utc-time'
}
initTime(settings, self) {
$('*[' + settings.attr + ']').each(function (t) {
var timefield = $(this);
var sourcevalue = timefield.attr(settings.attr);
......@@ -63,8 +74,8 @@
}
var date = new Date(timevalue);
}
var text = timeSince(date, settings);
if(settings.disableAgo) {
var text = self.timeSince(date, settings);
if (settings.disableAgo) {
if (settings.format) {
text = date.Format(settings.format);
} else {
......@@ -81,33 +92,25 @@
});
};
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
$.fn.initUTCTime = function (settings) {
if(settings == null || settings == undefined) {
settings = { };
initFormat() {
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var loop = function () {
initTime(settings);
setTimeout(loop, 1000);
};
loop();
}
}(jQuery))
\ No newline at end of file
}
export { UtcTime }
\ No newline at end of file
const path = require('path');
module.exports = [
{
entry: './src/jquery-utc-time.js',
externals: { jquery: 'jQuery' },
mode: "production",
output: {
filename: 'jquery-utc-time.min.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'umd',
globalObject: 'this'
}
}
];
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment