GithubHelp home page GithubHelp logo

sd0e / utctorelative Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 76 KB

An npm ES module package which converts a UTC time in milliseconds to a relative time (e.g. Tomorrow, 16:00)

Home Page: https://www.npmjs.com/package/utctorelative

License: MIT License

TypeScript 100.00%
date javascript npm npm-module npm-package relative-time timezone utc

utctorelative's Introduction

utcToRelative

utcToRelative is a package which converts a UTC time (in milliseconds) into a local time string which is relative to the current date. The purpose is to provide the user with an easier to understand time, such as Tomorrow, 16:00 or Sunday, 16:00 rather than 7 August 2022, 16:00.

Installation

utcToRelative can be installed by running npm i --save utctorelative.

It can be used by writing import { utcToRelative } from 'utctorelative' and calling the function utcToRelative.

Parameters

utc

This is the UTC time in milliseconds from which the relative date string will be calculated.

This is passed as an integer (e.g. 1659885783755).

The default value is the current time in milliseconds.


hourFormat

This determines whether the time will be displayed in 12-hour or 24-hour time (e.g. 4:23 pm or 16:23 respectively).

This is passed as a string of either "12" or "24".

The default value is "24".


monthFormat

This determines whether the month will be displayed in a full or concatenated form (e.g. August or Aug respectively).

This is passed as a string of either "full" or "concat".

The default value is "full".


currentTime

This is the current UTC time in milliseconds, which will be compared with the utc parameter to create a relative date string. This only needs to be defined if you do not want to use the system time as the current time.

This is passed as an integer (e.g. 1659885783755).

The default value is the current time in milliseconds, according to the system time.

Examples

For the purpose of these examples, 1659885783755 is Sunday, 7 August 2022 at 4:23 pm.

The following table shows the output of the following code when it is run on the stated days:

import { utcToRelative } from 'utctorelative';

console.log(utcToRelative(1659885783755, '12'));
Date Output
20th July, 2021 7 August 2022, 4:23 pm
20th July, 2022 7 August, 4:23 pm
4th August, 2022 Sunday, 4:23 pm
6th August, 2022 Tomorrow, 4:23 pm
7th August, 2022 Today, 4:23 pm
8th August, 2022 Yesterday, 4:23 pm
10th August, 2022 Last Sunday, 4:23 pm
20th August, 2022 7 August, 4:23 pm
20th August, 2023 7 August 2022, 4:23 pm

utctorelative's People

Contributors

sd0e avatar

Watchers

 avatar  avatar

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.