1
0
Fork 0
forked from 0x2620/oxjs

fix Ox.parseDate()

This commit is contained in:
rolux 2011-05-26 09:18:59 +02:00
commit 13669aec63
3 changed files with 22 additions and 15 deletions

View file

@ -1450,20 +1450,28 @@ Ox.makeYear = function(date, utc) {
Ox.parseDate(f) Takes a string ('YYYY-MM-DD HH:MM:SS') and returns a date
str <s> string
utc <b|false> If true, Date is UTC
> +Ox.parseDate('1970-01-01 01:01:01')
3661000
> +Ox.parseDate('1970', true)
0
> Ox.parseDate('50', true).getUTCFullYear()
50
@*/
Ox.parseDate = function(str, utc) {
var def = [, 1, 1, 0, 0, 0];
val = /(\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/(str);
val.shift();
val = val.map(function(v, i) {
return v || def[i];
var date = new Date(),
defaults = [, 1, 1, 0, 0, 0],
values = /(\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/(str);
values.shift();
values = values.map(function(v, i) {
return v || defaults[i];
});
val[1]--;
return utc
? new Date(Date.UTC(val[0], val[1], val[2], val[3], val[4], val[5]))
: new Date(val[0], val[1], val[2], val[3], val[4], val[5])
values[1]--;
[
'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds'
].forEach(function(part, i) {
date = Ox['set' + part](date, values[i], utc);
});
return date;
};
//@ Ox.setDate <f> Set the day of a date, optionally UTC
@ -1490,12 +1498,9 @@ Ox.parseDate = function(str, utc) {
return Ox.makeDate(date)['get' + (utc ? 'UTC' : '') + noun]()
}
Ox['set' + noun] = function(date, num, utc) {
// new Date(date) makes a clone, so that
// setSomething() doesn't have side effects
return new Date(
Ox.makeDate(date)
)['set' + (utc ? 'UTC' : '') + noun](num);
// fixme: maybe we _want_ set to have side effects?
Ox.makeDate(date)['set' + (utc ? 'UTC' : '') + noun](num)
);
}
});