timezone-now
Version:
A lightweight NPM package to get the current time in any timezone using the Luxon library
125 lines (93 loc) • 2.96 kB
Markdown
A lightweight NPM package to get the current time in any timezone using the robust Luxon library.
```bash
npm install timezone-now
```
For global CLI installation:
```bash
npm install -g timezone-now
```
```javascript
const { timezoneNow } = require('timezone-now');
// Get current time in New York
console.log(timezoneNow('America/New_York')); // Example output: Dec 15, 2023, 8:30 PM
// Get current time in London
console.log(timezoneNow('Europe/London')); // Example output: Dec 16, 2023, 1:30 AM
```
```javascript
// Use predefined Luxon formats
console.log(timezoneNow('America/New_York', { format: 'DATE_FULL' }));
// Output: December 15, 2023
console.log(timezoneNow('Europe/London', { format: 'TIME_SIMPLE' }));
// Output: 1:30 PM
// Use custom format string
console.log(timezoneNow('Asia/Tokyo', { format: 'yyyy-MM-dd HH:mm:ss' }));
// Output: 2023-12-16 10:30:45
```
```javascript
// Specify a fallback timezone for invalid inputs
console.log(timezoneNow(null, { fallbackTimezone: 'UTC' }));
// Uses UTC as fallback
// Invalid timezone will fall back to UTC by default
console.log(timezoneNow('Invalid/Timezone'));
```
```javascript
const { getTimeDifference } = require('timezone-now');
const diff = getTimeDifference('America/New_York', 'Asia/Tokyo');
console.log(diff);
// Output: {
// hours: 14,
// minutes: 0,
// formatted: '+14:00',
// raw: 840
// }
```
After global installation, you can use the package from the command line:
```bash
timezone-now America/New_York
timezone-now America/New_York TIME_SIMPLE
timezone-now diff America/New_York Asia/Tokyo
```
- 🌎 Supports all IANA timezone identifiers
- 🚀 Lightweight and easy to use
- ⚡️ Built on top of the reliable Luxon library
- 🛡️ Includes error handling for invalid timezones
- 📝 Customizable output formats
- 🔄 Fallback timezone support
- 💻 CLI support
- ⏱️ Time difference calculation
The package handles various error cases:
- When no timezone is provided (falls back to UTC)
- When an invalid timezone is provided (falls back to specified fallback timezone)
- When invalid format is provided
```javascript
// Will use fallback timezone (UTC)
timezoneNow();
// Will throw an error if fallback also fails
timezoneNow('Invalid/Timezone', { fallbackTimezone: 'Also/Invalid' });
```
- `America/New_York` - New York
- `America/Los_Angeles` - Los Angeles
- `Europe/London` - London
- `Europe/Paris` - Paris
- `Asia/Tokyo` - Tokyo
- `Asia/Dubai` - Dubai
- `Australia/Sydney` - Sydney
- `Pacific/Auckland` - Auckland
MIT
Contributions, issues, and feature requests are welcome!