taylorhakes / fecha Goto Github PK
View Code? Open in Web Editor NEWLightweight and simple JS date formatting and parsing
License: MIT License
Lightweight and simple JS date formatting and parsing
License: MIT License
When parsing strings containing zero offset timezone, timezone information is ignored.
see snippet:
fecha.parse = function (dateStr, format) {
var time, isValid, dateInfo, today, date, info, index;
if (!format) {
time = Date.parse(dateStr.replace(/\-/g, '/'));
if (!isNaN(time)) {
return new Date(time);
} else {
return false;
}
} else {
I have tried the following code on both Chrome and Safari:
Date.parse("2015-07-18T07:53:14.000Z")
// 1437205994000
Date.parse("2015/07/18T07:53:14.000Z")
// NaN
As you can see, after replace '-' to '/', Date.parse
cannot parse the date string correctly, then the function gives me 'false'.
The javascript document says:
Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause Date.parse() to return NaN.
Obviously, the "2015-07-18T07:53:14.000Z"
is legal while "2015/07/18T07:53:14.000Z"
is illegal.
I don't know why you must change - to /.
to be removed
Really weird but it stripped out all reference to the literals (eg: 'D MMM YYYY [at] h:mma') so that it was outputting [pmt] or [amt] using the 'a' as a token... I took the fecha.js file over to an online minifyer and it worked fine... Can you guys please correct this and push the working 'official' min.js file to git.
Cheers!
Richard :o)
the latest version is only 1.2.2 (from 2016-01-07)
Readme says that one can use isoDateTime
and isoDate
format.
b3eb209 commit adds support of it but latest released version (4.1.0) does not have them.
Can fecha do moment([2007, 0, 29]).fromNow(); // 4 years ago
?
https://momentjs.com/docs/#/displaying/fromnow/
Thanks
Hi,
is there a way to display year part of the date only when the year is not the current year?
example:
Jan 27
but: Dec 31, 2016
Hi, I'm from cdnjs, we host js
, css
and img
lib for browser usage. Recently we want to host your popular lib on https://cdnjs.com, just want to confirm that if we also need to host fetch.spec.js
because I saw them in some old versions, but unminfied.
Thank you!
Looks like this has been previously fixed in #39, but broken as of the 3.0.0 release.
fecha.format(new Date(), 'YYYY-MM-DD[T]HH:mm:ss[Z]')
Produces:
2016-09-16[T]15:00:35[Z]
Expected:
2016-09-16T15:00:35Z
When i don't use the separators between the tokens, it will lead on NodeJS and Chrome into an crash. Please fix or report this issue.
const fecha = require('fecha');
console.log(fecha.format(new Date(), 'hh:mm'));
Example:
Time now: 05:00 - right
fecha.format: 06:00 - wrong
Daylight Saving Time has been permanently abolished in Brazil, according to the Senate bulletin of April 25, 2019.
Why not return null instead of false within parse ?
Format method is not formatting the date correctly
fecha.format(new Date(2019, 12, 31), 'MM/DD/YYYY')
returns 01/31/2020
instead of 12/31/2019
Fecha throws error when trying to parse something like:
fecha.parse('2019-04-12T15:53', 'YYYY-MM-DD[T]HH:mm')
(bad format)
I get a wild error when I import the library with RequireJS, I see that evaluate require instead of define in the UMD pattern
if (typeof module !== 'undefined' && module.exports) {
module.exports = fecha;
} else if (typeof require !== 'undefined' && require.amd) {
define(function () {
return fecha;
});
} else {
main.fecha = fecha;
}
...
define(['bower_components/fecha/fecha'], function(fecha) {
typeof fecha === 'undefined'; // true
typeof window.fecha === 'undefined'; // true
window.fecha.parse('2015-01-01'); // it's works
fecha.parse('2015-01-01'); // error
});
whit define instead of require
if (typeof module !== 'undefined' && module.exports) {
module.exports = fecha;
} else if (typeof define !== 'undefined' && define.amd) {
define(function () {
return fecha;
});
} else {
main.fecha = fecha;
}
...
define(['bower_components/fecha/fecha'], function(fecha) {
typeof fecha === 'undefined'; // false
typeof window.fecha === 'undefined'; // true
// window.fecha.parse('2015-01-01'); // error
fecha.parse('2015-01-01'); // it's works
});
Token 'Z' produces 'Z' as an output (i.e. it is not recognized).
Token 'ZZ" produces time offset without :
:
console.log(fecha.format(new Date("2017-03-11T02:30:00.000+07:00"), "ZZ"));
// +0100
It also uses current machine timezone. Because new Date().getTimezoneOffset();
always returns current machine timezone.
fecha version 4.1.0 .
Is it possible to set a custom locale? If user has the browser's language set to english he might change the website's language to another language. How can I set a custom locale when formatting the date using Fecha?
Snitram
Same as moment.js: https://momentjs.com/docs/#/parsing/string-formats/
it's awesome that date-fm have resolve function
Hi,
I'm getting the following error when trying to call fecha.parse
TypeError: dateStr.match is not a function
current is:
export function format(dateObj: Date | number, mask: string, i18nSettings?: i18nSettings): string;
export function parse(dateStr: string, format: string, i18nSettings?: i18nSettings): Date | null;
should be:
type Fecha = {
format: (dateObj: Date | number, mask: string, i18nSettings?: i18nSettings): string;
parse: (dateStr: string, format: string, i18nSettings?: i18nSettings): Date | null;
}
export default Fecha;
Currently fecha
supports the ZZ
formatting token, but it would be nice if we also had Z
which is defined in moment:
Token | Output |
---|---|
Z | -07:00 -06:00 ... +06:00 +07:00 |
ZZ | -0700 -0600 ... +0600 +0700 |
I came across this issue because js-joda currently doesn't parse it without the colon (js-joda/js-joda#377).
EDIT: RFC3339 requires the colon, so yea, we do need this.
Input: "Friday 15th Jul"
Format "dddd Do MMM"
Output: 2016-01-15
Expected output: 2016-07-15
I may have missed it, but is there a way to format("x") or format("unix") to return the date as a unix timestamp?
if I parse 12/31/2019
with the format DD/MM/YYYY
I dont get an error, I get the date 12/07/2021
.
not cool!!
Hello
Thank you for this lightweight library, I really like it.
I found that there is no info on how to import it.
It's quite obvious for an experienced developer, but let's make it simple for newbies also.
I've prepared a simple MR, could you check it? If you will approve, please don't forget to update it on the page of the project for NPM, because most of the people open the page of the project there.
MR: #68
https://unpkg.com/[email protected]/
package.json
points for the module version at src/fecha.js
, this file is not included in the package.
hi , do it support jalali date ?
I'm trying to migrate from fecha 2.3.3 to version 3.0.2. For my application itself, the new version works perfectly. However, I'm not able to run any unit tests that uses fecha directly or indirectly.
I created a minimal TypeScript project for demonstrating the issue:
src/example.ts
import fecha from "fecha"; // import * as fecha from "fecha" for version 2.3.3
export function output() { return fecha.format(new Date(), "YYYY"); }
src/app.ts
import {output} from "./example";
console.log(output());
src/app.test,ts
import {output} from "./example";
it("output", () => expect(output()).toEqual("2019"));
package.json
{
"private": true,
"dependencies": {
"fecha": "3.0.2"
},
"devDependencies": {
"@types/jest": "23.3.12",
"awesome-typescript-loader": "5.2.1",
"jest": "23.6.0",
"ts-jest": "23.10.5",
"typescript": "3.2.2",
"webpack": "4.28.3",
"webpack-cli": "3.2.1"
},
"jest": {
"preset": "ts-jest"
}
}
tsconfig.json
{ "include": [ "./src/" ] }
webpack.config.js
module.exports = {
entry: "./src/app.ts",
output: { filename: "bundle.js" },
resolve: { extensions: [".ts"] },
module: { rules: [ { test: /\.ts$/, use: { loader: "awesome-typescript-loader" } } ] }
}
I can compile (yarn run webpack -p
) and execute the bundle (node ./dist/bundle.js
) successfully.
However, when I try to run the tests (yarn run jest
), I get an error:
FAIL src/app.test.ts
× output (7ms)
● output
TypeError: Cannot read property 'format' of undefined
1 | import fecha from "fecha";
> 2 | export function output() { return fecha.format(new Date(), "YYYY"); }
| ^
at Object.output (src/example.ts:2:41)
at Object.<anonymous> (src/app.test.ts:4:27)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 2.074s, estimated 3s
Ran all test suites.
error Command failed with exit code 1.
The same error occurs also with other testing frameworks like Jasmine. With fecha 2.3.3, it had worked.
Hello!
I was trying to make the following instruction and got the following output.
fecha.parse('2015-02-29')
// Sun Mar 01 2015 00:00:00 GMT+0000 (WET)
Souldn't return false
instead of the next day?
moment.js allows to use literals in formatting masks, it would be nice to have a similar feature in fecha.
Example:
fecha.format(new Date(2001, 2, 5, 6, 7, 2, 5), '[on] MM-DD-YYYY [at] HH:mm');
// 'on 03-05-2001 at 06:07'
PR that implements the feature #17
Winston@3, AWS Linux instance, Javascript
What's the feature?
Add microseconds precision to the timestamp format. something like:
timestamp({ format: 'YYYY-MM-DD HH:mm:ss.' })
What problem is the feature intended to solve?
My use case is logging (I'm using Winston which in turn is using fecha under the hood for timestamp formatting). Log aggregation of logs created by many instances of different microservices requires a precision level higher than milliseconds in order to better sort the logs based on their timestamp.
Is the absence of this feature blocking you or your team? If so, how?
Not a blocker, but a very desirable feature.
Is this feature similar to an existing feature in another tool?
No that I know of.
Is this a feature you're prepared to implement, with support from us?
I'd be willing to look into it.
Very nice to see that typescript typings are available, sadly for people to be able to consume these, they need to be published to npm, together with the rest of the fecha bundle.
Hi,
It's possible to add tjhe possibility to force the format function with specific timezone ?
Actually, If I pass an UTC date in my brwoser, the formatted date is converted into the local timezone (Europe/Paris).
But, I want show the date in UTC format (or get a part of this date in the UTC timezone like juste the hour part).
Thanks
Hey,
Can you please add support for timezone names such as GMT
, UTC
, EST
, BST
and so on?
I really love your library and the fact that is so small and performant and that would help me greatly to parse logs from various servers around the world.
Thank you!
I don't know if it's a bug or feature, but I was depending on fecha to set date-part to HTML5 date input, which expects date in format YYYY-MM-DD, where YYYY means 4-digit year.
However, fecha substitutes YYYY with Date.getFullYear() which is as long as it takes...
So the following code:
var date = new Date('0999-10-09T22:00:00.000Z');
console.log(fecha.format(date, 'YYYY-MM-DD'));
Produces "999-10-10" instead of "0999-10-10".
Is it intended behavior?
Add i18n settings as 3rd param to fecha.parse
and fecha.format
. This would allow people to change i18n settings without doing it globallly on fecha.i18n
. fecha.i18n
will still be available for global changes.
I have tested the parse method for different time zones and see that
parse('1990-01-01', 'YYYY-MM-DD')
returns null when in the Lima/Peru timezone. I have modified the test for the first day of month for 1st of January 1990 and this is the output of the test
x first day of the month
expected 0 to equal 631170000000
I did it on a Ubuntu 18.04 VM but cannot reproduce the error on more negative timezones.
First up, awesome library. Thanks for all the hard work.
This is more of a usability/understandability issue. Right now dates are being parsed 0 based. This means that the following happens
fecha.parse('2010-02-02', 'YYYY-MM-DD');// 02 March 2010
fecha.parse('2010-03-02', 'YYYY-MM-DD');// 02 April 2010
fecha.parse('2010-04-02', 'YYYY-MM-DD');// 02 May 2010
The documentation seems to suggest that this is expected behavior
fecha.parse('10-12-10 14:11:12', 'YY-MM-DD HH:mm:ss'); // new Date(2010, 11, 10, 14, 11, 12)
As you can imagine this is a land mine for a any developer (including myself) that didn't read the fine print.
My recommendation is that is should parse the dates non-zero based so that its more predictable
Hey guys,
Could you please write an example how to use your library?
I'd expected to get a false
return or an exception when I called the fecha.parse
method with an invalid and a malformed dates. But it parsed them successfully. Is this the desired result?
Invalid: Date parts are not in the valid ranges (12 >= Month >= 1, 31>= Day>= 1, 23>=Hour>= 0 ...)
fecha.parse('2016-44-99', 'YYYY-MM-DD'); // Sat Nov 12 2016 21:39:00 GMT+0300
fecha.parse('2016-11-11 44:44', 'YYYY-MM-DD HH:mm'); // Sat Nov 12 2016 20:44:00 GMT+0300
Malformed: Supplied date string does not match the format
fecha.parse('2016-1-1', 'YYYY-MM-DD'); // Fri Jan 01 2016 01:00:00 GMT+0400
fecha.parse('2016-11-11 3:4', 'YYYY-MM-DD HH:mm'); // Fri Nov 11 2016 03:04:00 GMT+0300
Invalid & Malformed:
fecha.parse('2016-9876-123', 'YYYY-MM-DD'); // Tue Apr 16 2024 00:00:00 GMT+0300
fecha.parse('2016-11-11 4433:3344', 'YYYY-MM-DD HH:mm'); // Sat Nov 12 2016 20:33:00 GMT+0300
Tested on: Google Chrome Version 54.0.2840.71 m (Windows 10)
Thanks,
Type definitions for typescript?
Moment.js is so prevalent that it would be nice to see a section at the top of the README that tells me why I should consider this library instead.
I mistakenly passed in a bad format 'YYY-MM-DD' (year was supposed to be 'YYYY') to the parse function. Took me a little to debug. Perhaps throwing an error here might help:
Lines 292 to 295 in 50fdd35
Maybe something like: "Date string does not fit format" ?
Is it possible to output 2 date values, first will be the date & time fecha was executed and then the other output is the date & time plus 3 days?
It would be great if formatting tokens can be extensible. For my use case, I want a token for showing year, only if it's not equal to the current year. Other tokens can be relative time (e.g. 3 minutes ago). I don't know if the implementation allows such extensibility but it would be nice to support such feature.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.