1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-02-18 20:10:02 +00:00

Merge pull request #1229 from tobibeer/fix-1010

fixes #1010 sequential date formatstrings
This commit is contained in:
Jeremy Ruston 2014-12-15 17:26:31 +00:00
commit d031a93c6d

View File

@ -176,89 +176,114 @@ exports.slowInSlowOut = function(t) {
}; };
exports.formatDateString = function(date,template) { exports.formatDateString = function(date,template) {
var t = template; var match,
t = t.replace(/0hh12/g,function() { result="",
return $tw.utils.pad($tw.utils.getHours12(date)); t = template,
}); matches = [
t = t.replace(/hh12/g,function() { [/^0hh12/, function() {
return $tw.utils.getHours12(date); return $tw.utils.pad($tw.utils.getHours12(date));
}); }],
t = t.replace(/0hh/g,function() { [/^wYYYY/, function() {
return $tw.utils.pad(date.getHours()); return $tw.utils.getYearForWeekNo(date);
}); }],
t = t.replace(/hh/g,function() { [/^hh12/, function() {
return date.getHours(); return $tw.utils.getHours12(date);
}); }],
t = t.replace(/mmm/g,function() { [/^DDth/, function() {
return $tw.language.getString("Date/Short/Month/" + (date.getMonth() + 1)); return date.getDate() + $tw.utils.getDaySuffix(date);
}); }],
t = t.replace(/0mm/g,function() { [/^YYYY/, function() {
return $tw.utils.pad(date.getMinutes()); return date.getFullYear();
}); }],
t = t.replace(/mm/g,function() { [/^0hh/, function() {
return date.getMinutes(); return $tw.utils.pad(date.getHours());
}); }],
t = t.replace(/0ss/g,function() { [/^0mm/, function() {
return $tw.utils.pad(date.getSeconds()); return $tw.utils.pad(date.getMinutes());
}); }],
t = t.replace(/ss/g,function() { [/^0ss/, function() {
return date.getSeconds(); return $tw.utils.pad(date.getSeconds());
}); }],
t = t.replace(/[ap]m/g,function() { [/^0DD/, function() {
return $tw.utils.getAmPm(date).toLowerCase(); return $tw.utils.pad(date.getDate());
}); }],
t = t.replace(/[AP]M/g,function() { [/^0MM/, function() {
return $tw.utils.getAmPm(date).toUpperCase(); return $tw.utils.pad(date.getMonth()+1);
}); }],
t = t.replace(/wYYYY/g,function() { [/^0WW/, function() {
return $tw.utils.getYearForWeekNo(date); return $tw.utils.pad($tw.utils.getWeek(date));
}); }],
t = t.replace(/wYY/g,function() { [/^ddd/, function() {
return $tw.utils.pad($tw.utils.getYearForWeekNo(date)-2000); return $tw.language.getString("Date/Short/Day/" + date.getDay());
}); }],
t = t.replace(/YYYY/g,function() { [/^mmm/, function() {
return date.getFullYear(); return $tw.language.getString("Date/Short/Month/" + (date.getMonth() + 1));
}); }],
t = t.replace(/YY/g,function() { [/^DDD/, function() {
return $tw.utils.pad(date.getFullYear()-2000); return $tw.language.getString("Date/Long/Day/" + date.getDay());
}); }],
t = t.replace(/MMM/g,function() { [/^MMM/, function() {
return $tw.language.getString("Date/Long/Month/" + (date.getMonth() + 1)); return $tw.language.getString("Date/Long/Month/" + (date.getMonth() + 1));
}); }],
t = t.replace(/0MM/g,function() { [/^TZD/, function() {
return $tw.utils.pad(date.getMonth()+1); var tz = date.getTimezoneOffset(),
}); atz = Math.abs(tz);
t = t.replace(/MM/g,function() { return (tz < 0 ? '+' : '-') + $tw.utils.pad(Math.floor(atz / 60)) + ':' + $tw.utils.pad(atz % 60);
return date.getMonth() + 1; }],
}); [/^wYY/, function() {
t = t.replace(/0WW/g,function() { return $tw.utils.pad($tw.utils.getYearForWeekNo(date)-2000);
return $tw.utils.pad($tw.utils.getWeek(date)); }],
}); [/^[ap]m/, function() {
t = t.replace(/WW/g,function() { return $tw.utils.getAmPm(date).toLowerCase();
return $tw.utils.getWeek(date); }],
}); [/^hh/, function() {
t = t.replace(/DDD/g,function() { return date.getHours();
return $tw.language.getString("Date/Long/Day/" + date.getDay()); }],
}); [/^mm/, function() {
t = t.replace(/ddd/g,function() { return date.getMinutes();
return $tw.language.getString("Date/Short/Day/" + date.getDay()); }],
}); [/^ss/, function() {
t = t.replace(/0DD/g,function() { return date.getSeconds();
return $tw.utils.pad(date.getDate()); }],
}); [/^[AP]M/, function() {
t = t.replace(/DDth/g,function() { return $tw.utils.getAmPm(date).toUpperCase();
return date.getDate() + $tw.utils.getDaySuffix(date); }],
}); [/^DD/, function() {
t = t.replace(/DD/g,function() { return date.getDate();
return date.getDate(); }],
}); [/^MM/, function() {
t = t.replace(/TZD/g,function() { return date.getMonth() + 1;
var tz = date.getTimezoneOffset(), }],
atz = Math.abs(tz); [/^WW/, function() {
return (tz < 0 ? '+' : '-') + $tw.utils.pad(Math.floor(atz / 60)) + ':' + $tw.utils.pad(atz % 60); return $tw.utils.getWeek(date);
}); }],
t = t.replace(/\\(.)/g,"$1"); [/^YY/, function() {
return t; return $tw.utils.pad(date.getFullYear()-2000);
}]
];
while(t.length){
match = "";
$tw.utils.each(matches, function(m) {
var r,l;
if(t.match(m[0])) {
match = m[1].call();
r = m[0].toString(),
l = r.length - 3;
l = r.substr(2,1) == "[" ? l - 3 : l;
t = t.substr(l);
return false;
}
});
if(match) {
result += match;
} else {
result += t.charAt(0);
t = t.substr(1);
}
}
result = result.replace(/\\(.)/g,"$1");
return result;
}; };
exports.getAmPm = function(date) { exports.getAmPm = function(date) {
@ -587,4 +612,4 @@ exports.timer = function(base) {
return m; return m;
}; };
})(); })();