npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datetime-helper

v1.0.0

Published

datetime to unix-time, datetime add, datetime diff, datetime formatter...

Downloads

23

Readme

@jkinfeng/datetime-helper

Datetime helper for JavaScript

NPM version NPM Downloads MIT License

JavaScript Date objects represent a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC. The getTime() method returns the number of milliseconds since the Unix Epoch. JavaScript uses milliseconds as the unit of measurement, whereas Unix Time is in seconds. getTime() always uses UTC for time representation. For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone. So use the dateObj.getUnixTimeSec() uniform standard in seconds save the time(integer number) to database or any storage You don't need to think about timezone

Installation

npm i datetime-helper

Basic usage:

// node.js
// const dth = require('datetime-helper');

// web browser
// <script src='___js_path___/datetime-helper/index.js'></script>
// __DateTime.getUnixTimeSec(...)

API Reference

Syntax

dth.getUnixTimeSec($dateTime, $ms = false)

The getUnixTimeSec() method returns the number of seconds since the Unix Epoch.

| Param | Type | Default Value | Description | | --- | --- | --- | --- | | <$dateTime> | Object or String or Number | | Date Object or A string of RFC2822 or ISO 8601 date or A Number | | [$ms] | Boolean | false | true: in millisecondfalse: in second |

Example:

const dth = require('datetime-helper');
dth.getUnixTimeSec(new Date());
dth.getUnixTimeSec(1592494318);
dth.getUnixTimeSec(1592494318856, true);
dth.getUnixTimeSec(Date.now(), true);
dth.getUnixTimeSec('1592494318');
dth.getUnixTimeSec('1592494318856', true);
dth.getUnixTimeSec('1970-01-01');  // A string in RFC2822 or ISO 8601 date format
dth.getUnixTimeSec('1970-01-01T00:00:00.000Z');

dth.getDateZeroTime($dateTime, $ms = false)

The getUnixTimeSec() method returns the number of seconds since the Unix Epoch.

| Param | Type | Default Value | Description | | --- | --- | --- | --- | | <$dateTime> | Object or String or Number | | Date Object or A string of RFC2822 or ISO 8601 date or A Number | | [$ms] | Boolean | false | true: in millisecondfalse: in second |

Example:

const dth = require('datetime-helper');
dth.getDateZeroTime(new Date());
dth.getDateZeroTime(1592494318);
dth.getDateZeroTime(1592494318856, true);
dth.getDateZeroTime(Date.now(), true);
dth.getDateZeroTime('1592494318');
dth.getDateZeroTime('1592494318856', true);
dth.getDateZeroTime('1970-01-01');  // A string in RFC2822 or ISO 8601 date format
dth.getDateZeroTime('1970-01-01T00:00:00.000Z');

dth.dateAdd($date, $days)

The dateAdd() method returns a Date Object of JavaScript.

| Param | Type | Default Value | Description | | --- | --- | --- | --- | | <$date> | Date Object | | | | <$days> | Integer | | |

Example:

const dth = require('datetime-helper');
dth.dateAdd(new Date(), 100);
dth.dateAdd(new Date(), -100);

dth.dateDiff($start_date, $end_date)

The dateDiff() method returns an Object of JavaScript. when 'Invalid Date' then returns 'Invalid Start Date' or 'Invalid End Date'

| Param | Type | Default Value | Description | | --- | --- | --- | --- | | <$start_date> | Object or String or Number | | Date Object or A string of RFC2822 or ISO 8601 date or A Number | | <$end_date> | Object or String or Number | | Date Object or A string of RFC2822 or ISO 8601 date or A Number |

Return an Object

// {
//    pof: boolean,  // pass or future, 
//                      false: $start_date >= $end_date,
//                      true $start_date < $end_date
//    val: integer (in sec), // the number of seconds between two date
//    days: string(float.toFix(2)),  // about how many days
//    day: integer, // specific days
//    hour: integer,  // specific hour
//    min: integer,  // specific min
//    sec: integer  // specific sec
// }

Example:

const dth = require('datetime-helper');
dth.dateDiff(new Date('1970-01-01'), new Date('1971-01-01'));
dth.dateDiff(Date.now() / 1000, new Date('1971-01-01'));
dth.dateDiff(1592494318, 1602494318);
dth.dateDiff('1592494318', '1582494318');

dth.format($date, $pattern = 'YYYY-MM-DD HH:mm:ss')

The format() method return format a Date instance to string.

| Param | Type | Default Value | Description | | --- | --- | --- | --- | | <$date> | Date Object | | | | [$pattern] | String | YYYY-MM-DD HH:mm:ss | YYYY: full yearYY: yearMM: monthDD: dayHH: hourmm: minss: secms: millisecond |

Example:

const dth = require('datetime-helper');
dth.format(new Date());
dth.format(new Date(), 'YYYY/MM/DD HH/mm/ss/ms');
dth.format(new Date(), 'YYYY年MM年DD日HH小时mm分ss秒ms毫秒');
dth.format(new Date(), 'MM @ DD @ YYYY @ what the fun~~ HH ~~ mm ~~ ss ~~ ms');

Contributing

Please submit all issues and pull requests to the jkinfeng/datetime-helper repository!

Support

If you have any problem or suggestion please open an issue here.

License

MIT