Skip to content

jsany/date

Repository files navigation

Welcome to @jsany/date 👋

npm bundle size (scoped) GitHub code size in bytes Version Documentation Maintenance License: MIT

javascript date small es5 es6+

🚀 Install

npm install --save-dev @jsany/date

Usage

import { dateFormat, UTCTimestamp, UTC2Target, Target2UTC } from '@jsany/date';

// or const { dateFormat, UTCTimestamp, UTC2Target, Target2UTC } = require('@jsany/date');

const dateStr = '2019-01-02 14:08:59';
const bj = '2019-01-01 08:00:00';
const ist = '2019-01-01 05:30:00';
const utc = '2019-01-01 00:00:00';
const utc_unix = 1546300800000;

/**
 * @description dateFormat:格式化日期
 * @param {(object|string)} date - 日期对象/字符串
 * @param {string} mask - 日期格式,默认:mask='yyyy-MM-dd HH:mm:ss'
 * @returns {string} 返回格式化后的日期
 */
dateFormat(dateStr, 'd'); // "2"
dateFormat(dateStr, 'dd'); // "02"
dateFormat(dateStr, 'ddd'); // "Wed"
dateFormat(dateStr, 'dddd'); // "Wednesday"
dateFormat(dateStr, 'M'); // "1"
dateFormat(dateStr, 'MM'); // "01"
dateFormat(dateStr, 'MMM'); // "Jan"
dateFormat(dateStr, 'MMMM'); // "January"
dateFormat(dateStr, 'yy'); // "19"
dateFormat(dateStr, 'yyyy'); // "2019"
dateFormat(dateStr, 'h'); // "2"
dateFormat(dateStr, 'hh'); // "02"
dateFormat(dateStr, 'H'); // "14"
dateFormat(dateStr, 'HH'); // "14"
dateFormat(dateStr, 'm'); // "8"
dateFormat(dateStr, 'mm'); // "08"
dateFormat(dateStr, 's'); // "59"
dateFormat(dateStr, 'ss'); // "59"
dateFormat(dateStr, 'l'); // "000"
dateFormat(dateStr, 'L'); // "00"
dateFormat(dateStr, 'tt'); // "pm"
dateFormat(dateStr, 'TT'); // "PM"
dateFormat(dateStr, 'Z'); // "GMT"
dateFormat(dateStr, 'yyyy/MM/dd HH:mm:ss'); // "2019/01/02 14:08:59"

=============================================

/**
 * @description UTCTimestamp:获取utc时间戳
 * @param {string} date - utc日期对象/字符串,默认:当前时间
 * @returns {number} 返回utc时间戳
 */
console.log(UTCTimestamp(utc), utc_unix);

=============================================

/**
 * @description UTC2Target:utc时间转目标时区的时间,默认为utc时间转本地时间
 * @param {object|string} date - utc时间,日期对象/字符串
 * @param {number} timezone - 目标时区,默认:本地时区timezone=-480(中国时区+0800)
 * @param {*} mask - 日期格式,默认:mask='yyyy-MM-dd HH:mm:ss'
 * @returns {string} 返回目标时区的时间
 */
// utc转北京时间(-480)
UTC2Target(utc, -480); // "2019-01-01 08:00:00"
// utc转印度时间(-330)
UTC2Target(utc, -330); // "2019-01-01 05:30:00"

=============================================

/**
 * @description Target2UTC:目标时区的时间转utc时间,默认为本地时间转utc时间
 * @param {object|string} date - 目标时区时间,日期对象/字符串
 * @param {number} timezone - 目标时区,默认:本地时区timezone=-480(中国时区+0800)
 * @param {*} mask - 日期格式,默认:mask='yyyy-MM-dd HH:mm:ss'
 * @returns {string} 返回目标时区的utc时间
 */
// 北京时间转utc(-480)
Target2UTC(bj, -480); // "2019-01-01 00:00:00"
// 印度时间转utc(-330)
Target2UTC(ist, -330); // "2019-01-01 00:00:00"

Run tests

npm run test

👀 View online

view

Author

👤 jzg

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2019 jzg.
This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

About

📅 javascript transform date small

Topics

Resources

License

Stars

Watchers

Forks