123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
- factory((global.SillyDatetime = {}));
- }(this, function (exports) { 'use strict';
-
- function getDateObject(datetime) {
- var t = datetime instanceof Date ? datetime : new Date(datetime);
- if (!t.getDate()) {
- t = new Date();
- }
- return t;
- }
-
- function format(datetime, formatStr) {
- var t = getDateObject(datetime);
- var hours = undefined,
- o = undefined,
- i = 0;
- formatStr = formatStr || 'YYYY-MM-DD HH:mm:ss';
- hours = t.getHours();
- o = [['M+', t.getMonth() + 1], ['D+', t.getDate()],
-
- ['H+', hours],
-
- ['h+', hours > 12 ? hours - 12 : hours], ['m+', t.getMinutes()], ['s+', t.getSeconds()]];
-
- if (/(Y+)/.test(formatStr)) {
- formatStr = formatStr.replace(RegExp.$1, (t.getFullYear() + '').substr(4 - RegExp.$1.length));
- }
-
- for (; i < o.length; i++) {
- if (new RegExp('(' + o[i][0] + ')').test(formatStr)) {
- formatStr = formatStr.replace(RegExp.$1, RegExp.$1.length === 1 ? o[i][1] : ('00' + o[i][1]).substr(('' + o[i][1]).length));
- }
- }
-
- return formatStr.replace(/a/ig, hours > 11 ? 'pm' : 'am');
- }
-
-
- var LOCALE_EN = {
- future: 'in %s',
- past: '%s ago',
- s: 'a few seconds',
- mm: '%s minutes',
- hh: '%s hours',
- dd: '%s days',
- MM: '%s months',
- yy: '%s years'
- };
-
- var LOCALE_ZH_CN = {
- future: '%s内',
- past: '%s前',
- s: '几秒',
- mm: '%s分钟',
- hh: '%s小时',
- dd: '%s天',
- MM: '%s月',
- yy: '%s年'
- };
-
- var _curentLocale = undefined;
-
- function locate(arg) {
- var newLocale = undefined,
- prop = undefined;
- if (typeof arg === 'string') {
- newLocale = arg === 'zh-cn' ? LOCALE_ZH_CN : LOCALE_EN;
- } else {
- newLocale = arg;
- }
- if (!_curentLocale) {
- _curentLocale = {};
- }
- for (prop in newLocale) {
- if (newLocale.hasOwnProperty(prop) && typeof newLocale[prop] === 'string') {
- _curentLocale[prop] = newLocale[prop];
- }
- }
- }
-
-
- var DET_STD = [['yy', 31536e6],
- ['MM', 2592e6],
- ['dd', 864e5],
- ['hh', 36e5],
- ['mm', 6e4],
- ['s', 0]];
-
-
- function fromNow(datetime) {
- if (!_curentLocale) {
-
- locate('');
- }
- var det = +new Date() - +getDateObject(datetime);
- var format = undefined,
- str = undefined,
- i = 0,
- detDef = undefined,
- detDefVal = undefined;
- if (det < 0) {
- format = _curentLocale.future;
- det = -det;
- } else {
- format = _curentLocale.past;
- }
- for (; i < DET_STD.length; i++) {
- detDef = DET_STD[i];
- detDefVal = detDef[1];
- if (det >= detDefVal) {
- str = _curentLocale[detDef[0]].replace('%s', parseInt(det / detDefVal, 0) || 1);
- break;
- }
- }
- return format.replace('%s', str);
- }
- exports.format = format;
- exports.locate = locate;
- exports.fromNow = fromNow;
- }));
|