Git Product home page Git Product logo

Comments (5)

fingerpich avatar fingerpich commented on May 29, 2024 2

Dear amir mohammad

I think you have set the locale globally like moment.locale('fa')
try the following code

let fromNow = moment.from("2018-10-25T13:20:00+00:00", 'en').locale('fa').fromNow()
console.log(fromNow)

Demo

from jalali-moment.

pouriaMaleki avatar pouriaMaleki commented on May 29, 2024 2

Can we discuss more on this @fingerpich ?

I think as ISO Date is a standard date format, it might be better to not treat it as locale date, but a standard date in 'en' locale.

I say this because it's very common to use this standard to get date/time from db/server and show it in locale format, and using it like below, might add a lot of moment.from(time, 'en').locale('fa') into code instead of simply moment.from(time) when time is ISO standard and locale is already globally sat.

let fromNow = moment.from("2019-01-17T08:19:19.975Z", 'en').locale(currentActiveLocale).fromNow()
console.log(fromNow)

Yet I found nothing about this in moment docs, and didn't check other calendar implementations of moment.js.

I have created a branch with a sample code to fix this on my fork, but it's better to know your opinion about it, then I create pull request for that.

from jalali-moment.

pouriaMaleki avatar pouriaMaleki commented on May 29, 2024 1

I generally agree with you, in jalali-moment, 'fa' locale is using total different calendar, so parsing dates also happen in same 'fa' calendar (and locale).

Can we make this as an option? Like user can configure jalali-moment to use gregorian calendar for parser even when locale is set to 'fa' and formatter is using Persian calendar?

moment.locale('fa', { useGregorianParser: true });
// use it everywhere like:
moment("2019-01-17T08:19:19.975Z").format("YYYY MM DD"); // "1397 10 27"

This will save our UI code base a lot of repetitive code, as right now I have two options:

moment.locale(navigator.language); // lets assume it's 'fa'
// use it everywhere like
moment(new Date("2019-01-17T08:19:19.975Z")).format("YYYY MM DD"); // "1397 10 27"

or

moment.locale('en');
// use it everywhere like
moment("2019-01-17T08:19:19.975Z").locale(navigator.language).format("YYYY MM DD"); // when navigator.language is 'fa' -> "1397 10 27"


Or even it gets better if moment accepts other calendar implementations and gets configured by locale options like:

import moment from 'moment';
moment.locale('fa', { useNativeCalendarFormatter: true, useNativeCalendarParser: false });

and this will be huge benefit when we want to support users from Arabic countries with pretty native Islamic calendar.

// no need to import other moment plugin as npm packages 
moment.locale('ar', { useNativeCalendarFormatter: true, useNativeCalendarParser: false });

Because right now I have to create a wrapper around moment, jalali-moment and something like moment-hijri to support all these different calendars for MiddleEast users.
``

from jalali-moment.

fingerpich avatar fingerpich commented on May 29, 2024 1

Yes it is a good idea and helpful if we consider that without this option it should do the same thing as before. if you can create a pull request and i will help you.
About moment I think its more than this as you can see in this issue

from jalali-moment.

fingerpich avatar fingerpich commented on May 29, 2024

Thanks a lot
but as long as you didn't change the global locale you can use it as easy as the following code

let fromNow = moment("2019-01-17T08:19:19.975Z").locale('fa').fromNow();
console.log(fromNow)

you know it's a rare thing that you need to change global locale to fa for example when you have lots of code such as moment(specificDate) and specificDate are used to be a gregorian date but now it's going to be jalali date like the situation we have in converting a gregorian calendar to shamsi calendar, we don't want to change all those codes so we can easily change the global locale and it works.
So when you need to change the global locale to fa you have to use method from for parsing the gregorian dates.

from jalali-moment.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.