Comments (6)
Hey @eyeseast! Just to make sure I'm following 100% can you elaborate further on what's happening? Maybe an example of the exact input, (incorrect) output you're seeing and output you'd expect/want?
from journalize.
Hi! (Great library, btw.) Here's roughly what happens:
const d = new Date('2020-05-05') // editor puts this in a spreadsheet
apdate(d)
// May 4, 2020, because timezones
d.toUTCString()
// "Tue, 05 May 2020 00:00:00 GMT"
So what I'm proposing is a function alongside apdate
that does the same thing, but using getUTC*
methods. (It could also be a second argument to apdate
.)
from journalize.
Thanks, that helps!
This is tricky — Date
is wildly inconsistent in how it treats strings being passed to new Date
. AFAICT you actually get a UTC time-zeroed date when you do new Date('2020-05-05')
. So in that scenario, you're right — you would rather have the getUTC*()
outputs.
The original version of apdate
used to have parsing built-in, but it made it impossible for folks to have full control over how their Date objects were created. (For example, it used date-fns
v1's parse
to do this, but it'd crash and burn if someone tried to pass moment
Dates in.) So the current expectation is that any inputs have already been formatted as desired as Date objects. I am hesitant to add a variation of apdate
(and apmonth
and aptime
) that steps back from that.
We handle this by parsing any input ISO date strings with date-fns' parseISO
, and pipe that into apdate
. If you trust your inputs to be consistent you could parse that string into the right format without too much trouble (or a new dependency). This should give you what you expect no matter what time zone it runs in:
const input = '2020-05-05';
// split and coerce to numbers
const [year, month, day] = input.split('-').map(Number);
// month is zero-indexed!
const d = new Date(year, month - 1, day);
from journalize.
Here's more what I'm thinking, if this helps:
export default function apdateUTC(date = new Date()) {
const month = apmonthUTC(date); // assume a UTC version for now
const dayOfMonth = date.getUTCDate();
const year = date.getUTCFullYear();
return `${month} ${dayOfMonth}, ${year}`;
}
So it's the same function, but it gives you UTC instead of localized dates. (This is what I ended up doing in my own code.) I'm not worried about parsing at all here, just changing output.
from journalize.
Sorry, I kind of rambled there!
What I was getting at is this is happening because of a quirk of how passing a partial ISO-formatted date string into new Date
does something unexpected (gives you a local time Date at midnight UTC).
Instead of making a variation of apdate
, etc. that collaborates with that parsing quirk, I'd recommend ensuring your input Date more accurately represents what you're after - in this case, a local time Date at midnight local time, which parseISO
or that little snippet I provided above will do.
from journalize.
Alright, we might have to disagree on this, which is totally fine. Happy to deal with this particular collision of javascript and editors on my end. Appreciate the feedback.
from journalize.
Related Issues (20)
- Consider adding wordwrap from Django HOT 1
- intword() approx vs. exact HOT 1
- Move tests to GitHub Actions
- Natural Time
- "ordinal" should have a flag for spelling out 1-9 in AP style HOT 1
- Feature request: intcomma options HOT 1
- Migrate intcomma to Intl.NumberFormat HOT 2
- intword should support negative numbers HOT 1
- Should there be a tabular version of apmonth? (Jan, Mar, Nov, etc.)
- Find answer for date functions and year outputs
- Support for a precision-based round/floor/ceil function
- intcomma rounding HOT 1
- [feat] Ordinal parameter to return only suffix HOT 5
- Decide whether date methods continue to be locale sensitive
- Add `yesno` function
- Add `boolify` function (working title)
- aptime gives incorrect output at noon HOT 1
- Add TypeScript definitions HOT 1
- Consider adding json_script from Django HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from journalize.