kandi background
Explore Kits

luxon | A library for working with dates and times in JS | Date Time Utils library

 by   moment JavaScript Version: Current License: MIT

 by   moment JavaScript Version: Current License: MIT

Download this library from

kandi X-RAY | luxon Summary

luxon is a JavaScript library typically used in Utilities, Date Time Utils applications. luxon has no bugs, it has no vulnerabilities, it has a Permissive License and it has medium support. You can install using 'npm i @vizual/luxon' or download it from GitHub, npm.
Luxon is a library for working with dates and times in JavaScript.
Support
Support
Quality
Quality
Security
Security
License
License
Reuse
Reuse

kandi-support Support

  • luxon has a medium active ecosystem.
  • It has 12355 star(s) with 604 fork(s). There are 103 watchers for this library.
  • It had no major release in the last 12 months.
  • There are 92 open issues and 709 have been closed. On average issues are closed in 26 days. There are 9 open pull requests and 0 closed requests.
  • It has a neutral sentiment in the developer community.
  • The latest version of luxon is current.
luxon Support
Best in #Date Time Utils
Average in #Date Time Utils
luxon Support
Best in #Date Time Utils
Average in #Date Time Utils

quality kandi Quality

  • luxon has 0 bugs and 0 code smells.
luxon Quality
Best in #Date Time Utils
Average in #Date Time Utils
luxon Quality
Best in #Date Time Utils
Average in #Date Time Utils

securitySecurity

  • luxon has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • luxon code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.
luxon Security
Best in #Date Time Utils
Average in #Date Time Utils
luxon Security
Best in #Date Time Utils
Average in #Date Time Utils

license License

  • luxon is licensed under the MIT License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.
luxon License
Best in #Date Time Utils
Average in #Date Time Utils
luxon License
Best in #Date Time Utils
Average in #Date Time Utils

buildReuse

  • luxon releases are not available. You will need to build from source code and install.
  • Deployable package is available in npm.
  • Installation instructions are not available. Examples and code snippets are available.
  • luxon saves you 87 person hours of effort in developing the same functionality from scratch.
  • It has 71 lines of code, 0 functions and 93 files.
  • It has low code complexity. Code complexity directly impacts maintainability of the code.
luxon Reuse
Best in #Date Time Utils
Average in #Date Time Utils
luxon Reuse
Best in #Date Time Utils
Average in #Date Time Utils
Top functions reviewed by kandi - BETA

kandi has reviewed luxon and discovered the below as its top functions. This is intended to give you an instant insight into luxon implemented functionality, and help decide if they suit your requirements.

  • Convert a given token to a unit .
    • Define example .
      • Get the date from an array of tokens .
        • Creates a new DateTime instance
          • Returns a string representation of known formats .
            • Rollup input options
              • Converts an IO time to a string .
                • Format relative time for relative units .
                  • Computes the high order of the week for the specified date .
                    • Build a Babel library

                      Get all kandi verified functions for this library.

                      Get all kandi verified functions for this library.

                      luxon Key Features

                      DateTime, Duration, and Interval types.

                      Immutable, chainable, unambiguous API.

                      Parsing and formatting for common and custom formats.

                      Native time zone and Intl support (no locale or tz files).

                      Luxon

                      copy iconCopydownload iconDownload
                      DateTime.now().setZone("America/New_York").minus({ weeks: 1 }).endOf("day").toISO();
                      

                      i18next undefined is not an object (evaluating '_i18next.default.services.formatter.add')

                      copy iconCopydownload iconDownload
                      interpolation: {
                        escapeValue: false, // not needed for react as it escapes by default
                        format: (value, format, lng) => { // legacy usage
                          if (value instanceof Date) {
                            return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime[format])
                          }
                          return value;
                        }
                      },
                      

                      i18next backend loadPath fails to map the data

                      copy iconCopydownload iconDownload
                      // ...
                      loadPath: `http://localhost:5000/{{lng}}',
                      request: function (options, url, payload, callback) {
                        axios.get(url).then((res) => {
                            callback(null, { status: 200, data: res.data });
                        }).catch((err) => callback(err));
                      },
                      // ...
                      

                      React tabulator ref becomes null on rerendering

                      copy iconCopydownload iconDownload
                      const StaffCompetency = (props) => {
                      
                          // State Handling
                          const [state, dispatch] = useReducer(reducer, initialState);
                          
                          // Reference for the tabulator
                          const staffCompetencyTableRef = useRef(null);
                      
                          // Action to download workloads data as 'JSON'
                          const downloadAsJSON = () => {
                              staffCompetencyTableRef.current.download("json", "RTP_Staff_Competencies.json");
                          }
                      
                          /***
                           * ALL OTHER CODE
                           */
                      
                          return (
                              <>
                                  <h3 className="text-center"> Staff Competency </h3>
                                  <div>
                                      <Button variant="dark" onClick={() => downloadAsJSON()}>Download JSON</Button>{' '}
                                  </div>
                                  <div style={{clear: 'both'}}></div>
                                  <br></br>
                                  <ReactTabulator
                                      onRef={(r) => (staffCompetencyTableRef.current = r.current)}
                                      columns={staffCompetencyTableCoumns}
                                      options={staffCompetencyTableOptions}
                                  />
                                  <ChangeStaffCompetency
                                      show={state.changeStaffCompetencyShow}
                                      onHide={() => dispatch({ type: "changeStaffCompetencyShow", value: false })}
                                      staffID= {state.staffID}
                                      workflowName= {state.workflowName}
                                      competentTasks= {state.competentTasks}
                                      api={props.api}
                                      parentCallback = {handleCallback}
                                  />
                              </>
                          );
                      }
                      

                      Migrating from Moment to Luxon - (t) =&gt; moment(t).format('hh:mm') equivalent

                      copy iconCopydownload iconDownload
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>
                      const day = moment(1318781876406);
                      
                      function getFormatterByTimeInterval (timeInterval: number) {/* ... */}
                      
                      export const getFormatterByTimeInterval = (timeInterval: number) => {
                        // This formats as time and date information
                        if (timeInterval > 240) {
                          return TIME_FORMATTER.DAY_HOUR_PERIOD;
                        }
                        // This formats as time-only information
                        // Also, this will never match because any `timeInterval` value which is
                        // greater than 480 is also greater than 240, so the first conditional above
                        // will match and return before reaching this one
                        else if (timeInterval > 480) {
                          return TIME_FORMATTER.HOUR_PERIOD;
                        }
                        // Shorter than either of the above
                        // This formats as date-only information
                        else {
                          return TIME_FORMATTER.DAY;
                        }
                      }
                      
                      import {DateTime} from 'luxon';
                      
                      export const TIME_FORMATTER = {
                        HOUR: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                        DAY: (millis: number) => DateTime.fromMillis(millis).toFormat('L/dd'),
                        HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                        DAY_HOUR_PERIOD: (millis: number) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                        SEC: (millis: number) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      
                      type FormatterFn = (millis: number) => string;
                      
                      export const getFormatterByTimeInterval = (timeInterval: number): FormatterFn => {
                        let key: keyof typeof TIME_FORMATTER;
                        // I'm still not sure about these conditionals,
                        // but this order matches longest duration first
                        if (timeInterval > 480) key = 'HOUR_PERIOD';
                        else if (timeInterval > 240) key = 'DAY_HOUR_PERIOD';
                        else key = 'DAY';
                        return TIME_FORMATTER[key];
                      };
                      
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                        const formatter = getFormatterByTimeInterval(timeInterval);
                        console.log(formatter(millis));
                      }
                      
                      <script type="module">
                      
                      import {DateTime} from 'https://unpkg.com/luxon@2.3.1/src/luxon.js';
                      
                      export const TIME_FORMATTER = {
                          HOUR: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm'),
                          DAY: (millis) => DateTime.fromMillis(millis).toFormat('L/dd'),
                          HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a'),
                          DAY_HOUR_PERIOD: (millis) => DateTime.fromMillis(millis).toFormat('hh:mm a L/dd'),
                          SEC: (millis) => DateTime.fromMillis(millis).toFormat('HH:mm:ss'),
                      };
                      export const getFormatterByTimeInterval = (timeInterval) => {
                          let key;
                          // I'm still not sure about these conditionals,
                          // but this order matches longest duration first
                          if (timeInterval > 480)
                              key = 'HOUR_PERIOD';
                          else if (timeInterval > 240)
                              key = 'DAY_HOUR_PERIOD';
                          else
                              key = 'DAY';
                          return TIME_FORMATTER[key];
                      };
                      // The time you asked this question
                      const millis = DateTime.fromISO('2022-03-21T21:02:03Z').toMillis();
                      // Check each conditional range:
                      for (const timeInterval of [500, 300, 100]) {
                          const formatter = getFormatterByTimeInterval(timeInterval);
                          console.log(timeInterval, formatter(millis));
                      }
                      
                      </script>

                      Chart.js sum y values depending on time unit

                      copy iconCopydownload iconDownload
                      const ctx = document.getElementById("tests-chart");
                      const data =[
                        { x: "24/03/2022", y: 20 },
                        { x: "25/03/2022", y: 5 },
                        { x: "24/06/2022", y: 10 },
                        { x: "25/06/2022", y: 5 }
                       ];
                      
                      let updatedData =[];
                      let tempDateCollection =[];
                      
                      data.map((yData , index)=> {
                      
                        const formatedDate = moment(yData.x, "DD/MM/YYYY").format('MMM/YYYY');
                      
                        if(tempDateCollection.includes(formatedDate)){
                          const index = tempDateCollection.indexOf(formatedDate);
                          const element = updatedData[index];
                          updatedData[index] = {...element, y: element.y + yData.y}
                        } else{
                          tempDateCollection.push(formatedDate);
                          updatedData.push(yData);
                        }
                        
                      })
                      
                      const testsChart = new Chart(ctx, {
                          type: "bar",
                          data: {
                              datasets: [
                                  {
                                      label: 'Dataset 1',
                                      data:updatedData,
                                      backgroundColor: 'rgb(255, 99, 132)',
                                    },
                              ],
                          },
                          options: {
                              scales: {
                                  x: {
                                      type: "time",
                                      time: {
                                          parser: "dd/MM/yyyy",
                                          unit: "month",
                                          // Luxon format string
                                          tooltipFormat: "MM",
                                          round: 'month'
                                      },
                                  },
                              },
                          },
                      })
                      <script type = "text/JavaScript" src = " https://MomentJS.com/downloads/moment.js"></script>
                      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
                      <script src="https://cdn.jsdelivr.net/npm/luxon@2.3.1/build/global/luxon.min.js"></script>
                      <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@1.1.0/dist/chartjs-adapter-luxon.min.js"></script>
                      
                      <div>
                        <canvas id="tests-chart"></canvas>
                      </div>
                      const ctx = document.getElementById("tests-chart");
                      const data =[
                        { x: "24/03/2022", y: 20 },
                        { x: "25/03/2022", y: 5 },
                        { x: "24/06/2022", y: 10 },
                        { x: "25/06/2022", y: 5 }
                       ];
                      
                      let updatedData =[];
                      let tempDateCollection =[];
                      
                      data.map((yData , index)=> {
                      
                        const formatedDate = moment(yData.x, "DD/MM/YYYY").format('MMM/YYYY');
                      
                        if(tempDateCollection.includes(formatedDate)){
                          const index = tempDateCollection.indexOf(formatedDate);
                          const element = updatedData[index];
                          updatedData[index] = {...element, y: element.y + yData.y}
                        } else{
                          tempDateCollection.push(formatedDate);
                          updatedData.push(yData);
                        }
                        
                      })
                      
                      const testsChart = new Chart(ctx, {
                          type: "bar",
                          data: {
                              datasets: [
                                  {
                                      label: 'Dataset 1',
                                      data:updatedData,
                                      backgroundColor: 'rgb(255, 99, 132)',
                                    },
                              ],
                          },
                          options: {
                              scales: {
                                  x: {
                                      type: "time",
                                      time: {
                                          parser: "dd/MM/yyyy",
                                          unit: "month",
                                          // Luxon format string
                                          tooltipFormat: "MM",
                                          round: 'month'
                                      },
                                  },
                              },
                          },
                      })
                      <script type = "text/JavaScript" src = " https://MomentJS.com/downloads/moment.js"></script>
                      <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.7.1/chart.min.js"></script>
                      <script src="https://cdn.jsdelivr.net/npm/luxon@2.3.1/build/global/luxon.min.js"></script>
                      <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-luxon@1.1.0/dist/chartjs-adapter-luxon.min.js"></script>
                      
                      <div>
                        <canvas id="tests-chart"></canvas>
                      </div>

                      How to make 'ref' work for ReactTabulator

                      copy iconCopydownload iconDownload
                      import React, { useState, useEffect, Suspense, useRef } from "react";
                      import PropTypes from "prop-types";
                      
                      import "react-tabulator/lib/styles.css"; // default theme
                      import "react-tabulator/css/tabulator_midnight.min.css";
                      
                      import { Button } from "react-bootstrap";
                      import { ReactTabulator } from "react-tabulator";
                      
                      const luxon = require("luxon");
                      window.DateTime = luxon.DateTime;
                      
                      const FetchWorkloads = (props) => {
                        // Possible Task Status Options (for display purpose)
                        const taskStatusOptions = [
                          { id: "N", label: "New" },
                          { id: "P", label: "In Progress" },
                          { id: "C", label: "Completed" }
                        ];
                      
                        // Variable to hold the workloads data
                        const [workloadData, setWorkloadData] = useState([]);
                      
                        let workloadsTableRef = useRef(null);
                      
                        // Action to download workloads data as 'JSON'
                        const downloadAsJSON = () => {
                          workloadsTableRef.current.download("json", "RTP_Workloads.json");
                        };
                      
                        // Action to download workloads data as 'CSV'
                        const downloadAsCSV = () => {
                          // action
                        };
                      
                        // Action to download workloads data as 'PDF'
                        const downloadAsPDF = () => {
                          // action
                        };
                      
                        // Fetch the workloads from DB
                        const getWorkloadData = async () => {
                          // API Call
                        };
                      
                        // Workload Table Column Setup
                        const workloadsTableCoumns = [
                          // column setup
                        ];
                      
                        // Workload Table Options Setup
                        const workloadsTableOptions = {
                          index: "id",
                          layout: "fitColumns",
                          pagination: true,
                          paginationSize: 20,
                          placeholder: "No Workload Available"
                        };
                      
                        // useEffect call the actions inside it after rendering
                        useEffect(() => {
                          getWorkloadData();
                        }, []);
                      
                        return (
                          <>
                            <h3 className="text-center"> Radiotherapy Physics Workloads </h3>
                            <div>
                              <Button variant="dark" onClick={() => downloadAsJSON()}>
                                Download JSON
                              </Button>{" "}
                              <Button variant="dark" onClick={() => downloadAsCSV()}>
                                Download CSV
                              </Button>{" "}
                              <Button variant="dark" onClick={() => downloadAsPDF()}>
                                Download PDF
                              </Button>
                            </div>
                            <div style={{ clear: "both" }}></div>
                            <br></br>
                            <Suspense fallback={<p>Fetching workloads...</p>}>
                              <ReactTabulator
                                onRef={(r) => (workloadsTableRef = r)}
                                columns={workloadsTableCoumns}
                                data={workloadData}
                                options={workloadsTableOptions}
                              />
                            </Suspense>
                          </>
                        );
                      };
                      
                      FetchWorkloads.propTypes = {
                        api: PropTypes.object
                      };
                      
                      export default FetchWorkloads;
                      
                      

                      Obtain Duration of a quarter in Luxon

                      copy iconCopydownload iconDownload
                      const startDateOfQuarter = DateTime.fromFormat('3', 'q');
                      const endDateOfQuarter = startDateOfQuarter.endOf('quarter')
                      

                      Date Unit test passed locally but failed in Gitlab CD/CI pipeline

                      copy iconCopydownload iconDownload
                      const toDateString = 'Mon Feb 07 2022 00:00:00 GMT+0200 (Eastern European Standard Time)'
                      // ...
                      describe('formatTime', () => {
                        it('Should return 00:00 when there is no time', () => {
                          expect(formatTime(toDateString)).toBe('00:00')
                        })
                      
                      Expected: "00:00"
                      Received: "22:00"
                      
                      const toDateString = 'Mon Feb 07 2022 00:00:00 GMT+0200 (Eastern European Standard Time)'
                      // ...
                      describe('formatTime', () => {
                        it('Should return 00:00 when there is no time', () => {
                          expect(formatTime(toDateString)).toBe('00:00')
                        })
                      
                      Expected: "00:00"
                      Received: "22:00"
                      

                      Typescript undefined date validation

                      copy iconCopydownload iconDownload
                      export const dateToISOString = (date: Date | string | undefined): string => {
                        if (!date) {
                          /**
                           * handle the case when the caller passes undefined explicitly.
                           * Dependent on your use-case you may want to 
                           * - return a default text: e.g. "", "-", etc.
                           * - or throw Error('undefined date!');
                           */
                          return '-';
                        } else if (typeof date === "string") {
                          // then check if the input is a string
                          return date;
                        } else {
                          /**
                           * now typescript knows that the input is of type Date
                           * because you defined the input type as:  Date | string | undefined
                           * and you have already handled the undefined and string cases
                           */
                          return dateToStrings(date).toFormat(ISO_DATE_FORMAT);
                        }
                      };
                      

                      Difference between date-fns' isValid and luxon's isValid

                      copy iconCopydownload iconDownload
                      DateTime.fromJSDate(dateString).isValid
                      

                      Community Discussions

                      Trending Discussions on luxon
                      • i18next undefined is not an object (evaluating '_i18next.default.services.formatter.add')
                      • i18next backend loadPath fails to map the data
                      • ERROR in ./ordenesPago.js Module not found: Error: Can't resolve 'vue-confirm-dialog' in '/resources/js/administracion'
                      • Tsx luxon type of DateTime
                      • React tabulator ref becomes null on rerendering
                      • Migrating from Moment to Luxon - (t) =&gt; moment(t).format('hh:mm') equivalent
                      • Chart.js sum y values depending on time unit
                      • How to make 'ref' work for ReactTabulator
                      • Obtain Duration of a quarter in Luxon
                      • Date Unit test passed locally but failed in Gitlab CD/CI pipeline
                      Trending Discussions on luxon

                      QUESTION

                      i18next undefined is not an object (evaluating '_i18next.default.services.formatter.add')

                      Asked 2022-Mar-29 at 16:06

                      why I get this error message:

                      undefined is not an object (evaluating '_i18next.default.services.formatter.add')
                      

                      Code:

                      import i18next from "i18next";
                      import { initReactI18next } from "react-i18next";
                      import LanguageDetector from 'i18next-browser-languagedetector';
                      import HttpApi from 'i18next-http-backend';
                      import { DateTime } from 'luxon';
                      
                      i18next
                        .use(HttpApi)
                        .use(LanguageDetector)
                        .use(initReactI18next)
                        .init({
                          react: {
                            useSuspense: true
                          },
                          fallbackLng: 'en',
                          compatibilityJSON: 'v3',
                          lng: 'de',
                          debug: process.env.NODE_ENV === 'development',
                          backend: {
                            loadPath: `http://192.168.0.249:4000/public/locales/{{lng}}/translation.json`,
                          },
                          interpolation: {
                            escapeValue: false,
                          },
                          keySeparator: '.'
                        });
                      
                      // new usage
                      i18next.services.formatter.add('DATE_HUGE', (value, lng, options) => {
                        return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime.DATE_HUGE)
                      });
                      
                      export default i18next;
                      
                      

                      ANSWER

                      Answered 2022-Mar-29 at 16:06

                      You're probably not using i18next >= 21.3.0

                      in that case use the legacy formatting option: https://www.i18next.com/translation-function/formatting#legacy-format-function-i18next-less-than-21.3.0

                      interpolation: {
                        escapeValue: false, // not needed for react as it escapes by default
                        format: (value, format, lng) => { // legacy usage
                          if (value instanceof Date) {
                            return DateTime.fromJSDate(value).setLocale(lng).toLocaleString(DateTime[format])
                          }
                          return value;
                        }
                      },
                      

                      Source https://stackoverflow.com/questions/71665124

                      Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                      Vulnerabilities

                      No vulnerabilities reported

                      Install luxon

                      You can install using 'npm i @vizual/luxon' or download it from GitHub, npm.

                      Support

                      General documentationAPI docsQuick tourFor Moment usersWhy does Luxon exist?A quick demo

                      DOWNLOAD this Library from

                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                      over 430 million Knowledge Items
                      Find more libraries
                      Reuse Solution Kits and Libraries Curated by Popular Use Cases
                      Explore Kits

                      Save this library and start creating your kit

                      Explore Related Topics

                      Share this Page

                      share link
                      Consider Popular Date Time Utils Libraries
                      Try Top Libraries by moment
                      Compare Date Time Utils Libraries with Highest Support
                      Compare Date Time Utils Libraries with Highest Quality
                      Compare Date Time Utils Libraries with Highest Security
                      Compare Date Time Utils Libraries with Permissive License
                      Compare Date Time Utils Libraries with Highest Reuse
                      Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from
                      over 430 million Knowledge Items
                      Find more libraries
                      Reuse Solution Kits and Libraries Curated by Popular Use Cases
                      Explore Kits

                      Save this library and start creating your kit

                      • © 2022 Open Weaver Inc.