forked from 0x2620/oxjs
fix Ox.parseDate()
This commit is contained in:
parent
ab6a07d137
commit
13669aec63
3 changed files with 22 additions and 15 deletions
|
|
@ -161,7 +161,7 @@ Ox.Calendar = function(options, self) {
|
|||
var date = new Date();
|
||||
date.setUTCFullYear(Math.floor(i / 12) + 1970);
|
||||
date.setUTCMonth(Ox.mod(i, 12));
|
||||
date.setUTCDate(0);
|
||||
date.setUTCDate(0); // fixme: WTF??
|
||||
return date;
|
||||
//*/
|
||||
//return new Date(Date.UTC(Math.floor(i / 12) + 1970, Ox.mod(i, 12), 1));
|
||||
|
|
|
|||
33
source/Ox.js
33
source/Ox.js
|
|
@ -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)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue