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 🙏

© 2025 – Pkg Stats / Ryan Hefner

time-to-unix-converter

v1.1.0

Published

Convert date/time components with timezone support to Unix timestamps

Readme

Unix Time Converter

A TypeScript/JavaScript library for converting date/time components with timezone support to Unix timestamps.

npm version License: MIT

Features

Timezone Support: Handle both UTC offsets (+05:30, -08:00) and IANA timezone names (America/New_York, Asia/Kolkata, Asia/Tel_Aviv) ✅ DST Aware: Automatically handles Daylight Saving Time transitions ✅ Type Safe: Full TypeScript support with comprehensive type definitions ✅ Well Tested: 100% test coverage with extensive edge case validation ✅ Zero Dependencies: No external runtime dependencies ✅ Secure: Input validation and error handling built-in ✅ Clean Code: Well-documented with JSDoc comments

Installation

npm install time-to-unix-converter
yarn add time-to-unix-converter
pnpm add time-to-unix-converter

Quick Start

import { dateToUnixTime } from 'time-to-unix-converter';

// Using UTC offset
const timestamp = dateToUnixTime(2024, 12, 4, 14, 30, 0, '+05:30');
console.log(timestamp); // 1733309400

// Using IANA timezone
const nyTimestamp = dateToUnixTime(2024, 12, 4, 7, 30, 0, 'America/New_York');
console.log(nyTimestamp); // 1733309400

API Reference

dateToUnixTime(year, month, day, hour, minute, second, timezone, useMilliseconds?)

Converts date/time components to Unix timestamp.

Parameters:

  • year (number): Full year (e.g., 2024)
  • month (number): Month (1-12, where 1 = January)
  • day (number): Day of month (1-31)
  • hour (number): Hour (0-23)
  • minute (number): Minute (0-59)
  • second (number): Second (0-59)
  • timezone (string): UTC offset ("+05:30", "-08:00", "Z") or IANA timezone name ("America/New_York")
  • useMilliseconds (boolean, optional): If true, returns timestamp in milliseconds instead of seconds. Defaults to false.

Returns: Unix timestamp in seconds (number), or milliseconds if useMilliseconds is true

Throws: Error if inputs are invalid

dateToUnixTimeFromObject(input)

Convenience function that accepts an object instead of individual parameters.

Parameters:

  • input (DateTimeInput): Object containing all date/time components
interface DateTimeInput {
  year: number;
  month: number;
  day: number;
  hour: number;
  minute: number;
  second: number;
  timezone: string;
  useMilliseconds?: boolean;  // Optional: if true, returns milliseconds instead of seconds
}

getCurrentUnixTime()

Returns the current Unix timestamp.

Returns: Current Unix timestamp in seconds (number)

unixTimeToIso(unixTime)

Converts Unix timestamp back to ISO 8601 string.

Parameters:

  • unixTime (number): Unix timestamp in seconds

Returns: ISO 8601 formatted string in UTC (string)

Usage Examples

Example 1: UTC Offset Format

import { dateToUnixTime } from 'time-to-unix-converter';

// India Standard Time (UTC+5:30)
const istTimestamp = dateToUnixTime(2024, 12, 4, 18, 0, 0, '+05:30');
console.log(istTimestamp); // 1733309400

// Eastern Standard Time (UTC-5:00)
const estTimestamp = dateToUnixTime(2024, 12, 4, 7, 30, 0, '-05:00');
console.log(estTimestamp); // 1733309400

// UTC
const utcTimestamp = dateToUnixTime(2024, 12, 4, 12, 30, 0, 'Z');
console.log(utcTimestamp); // 1733309400

Example 2: IANA Timezone Names

import { dateToUnixTime } from 'time-to-unix-converter';

// New York (automatically handles EST/EDT)
const nyTime = dateToUnixTime(2024, 12, 4, 7, 30, 0, 'America/New_York');
console.log(nyTime); // 1733309400

// India
const indiaTime = dateToUnixTime(2024, 12, 4, 18, 0, 0, 'Asia/Kolkata');
console.log(indiaTime); // 1733309400

// London
const londonTime = dateToUnixTime(2024, 12, 4, 12, 30, 0, 'Europe/London');
console.log(londonTime); // 1733309400

Example 3: Object Input

import { dateToUnixTimeFromObject, DateTimeInput } from 'time-to-unix-converter';

const input: DateTimeInput = {
  year: 2024,
  month: 12,
  day: 4,
  hour: 14,
  minute: 30,
  second: 0,
  timezone: '+05:30'
};

const timestamp = dateToUnixTimeFromObject(input);
console.log(timestamp); // 1733309400

Example 4: Current Time and Conversion

import { getCurrentUnixTime, unixTimeToIso } from 'time-to-unix-converter';

// Get current timestamp
const now = getCurrentUnixTime();
console.log(now); // e.g., 1733309400

// Convert back to ISO string
const iso = unixTimeToIso(now);
console.log(iso); // e.g., "2024-12-04T12:30:00.000Z"

Example 5: Restaurant Ordering System (Per Diem Use Case)

import { dateToUnixTime } from 'time-to-unix-converter';

// Customer places order in their local timezone
const orderPlacedAt = dateToUnixTime(
  2024, 12, 4,  // Date
  14, 30, 0,     // Time: 2:30 PM
  'America/Los_Angeles'  // Customer's timezone
);

// Convert to restaurant's timezone for kitchen display
const restaurantTimezone = 'America/New_York';
const orderDate = new Date(orderPlacedAt * 1000);

console.log(`Order placed at Unix: ${orderPlacedAt}`);
console.log(`Order time (UTC): ${orderDate.toISOString()}`);
console.log(`Order time (Local): ${orderDate.toLocaleString('en-US', {
  timeZone: restaurantTimezone
})}`);

Example 6: Scheduling Future Orders

import { dateToUnixTime, unixTimeToIso } from 'time-to-unix-converter';

// Schedule order for pickup tomorrow at 12:00 PM EST
const pickupTime = dateToUnixTime(2024, 12, 5, 12, 0, 0, 'America/New_York');

// Store in database
const order = {
  orderId: 'ORD-12345',
  scheduledPickupTime: pickupTime,
  scheduledPickupTimeISO: unixTimeToIso(pickupTime)
};

console.log(order);
// {
//   orderId: 'ORD-12345',
//   scheduledPickupTime: 1733414400,
//   scheduledPickupTimeISO: '2024-12-05T17:00:00.000Z'
// }

Example 7: Using Milliseconds

import { dateToUnixTime } from 'time-to-unix-converter';

// Get timestamp in seconds (default)
const timestampSeconds = dateToUnixTime(2024, 12, 4, 14, 30, 0, '+05:30');
console.log(timestampSeconds); // 1733309400

// Get timestamp in milliseconds
const timestampMilliseconds = dateToUnixTime(2024, 12, 4, 14, 30, 0, '+05:30', true);
console.log(timestampMilliseconds); // 1733309400000

Example 8: Error Handling

import { dateToUnixTime } from 'time-to-unix-converter';

try {
  // This will throw an error - invalid month
  const timestamp = dateToUnixTime(2024, 13, 4, 14, 30, 0, '+05:30');
} catch (error) {
  console.error(error.message); // "Invalid month: 13. Must be between 1-12"
}

try {
  // This will throw an error - invalid timezone
  const timestamp = dateToUnixTime(2024, 12, 4, 14, 30, 0, 'Invalid/Timezone');
} catch (error) {
  console.error(error.message); // "Invalid timezone: Invalid/Timezone..."
}

Timezone Support

UTC Offset Format

  • "Z" - UTC
  • "+05:30" - 5 hours 30 minutes ahead of UTC
  • "-08:00" - 8 hours behind UTC

IANA Timezone Names (Recommended)

Automatically handles Daylight Saving Time:

  • "America/New_York" - Eastern Time
  • "America/Los_Angeles" - Pacific Time
  • "America/Chicago" - Central Time
  • "Europe/London" - British Time
  • "Asia/Kolkata" - India Standard Time
  • "Asia/Tokyo" - Japan Standard Time
  • And many more...

Full list of IANA timezones

Input Validation

The library performs comprehensive input validation:

  • Month: Must be 1-12
  • Day: Must be 1-31
  • Hour: Must be 0-23
  • Minute: Must be 0-59
  • Second: Must be 0-59
  • Timezone: Must be valid UTC offset or IANA timezone name
  • Date: Must be a valid date (e.g., Feb 30 will throw error)

Development

Setup

# Clone the repository
git clone https://github.com/perdiem/unix-time-converter.git
cd unix-time-converter

# Install dependencies
npm install

# Build
npm run build

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Watch mode for development
npm run test:watch

Project Structure

unix-time-converter/
├── src/
│   └── index.ts          # Main source file
├── tests/
│   └── index.test.ts     # Test suite
├── dist/                 # Compiled output (generated)
├── package.json
├── tsconfig.json
├── jest.config.js
└── README.md

Testing

The library has comprehensive test coverage including:

  • UTC offset conversions
  • IANA timezone conversions
  • DST handling
  • Input validation
  • Edge cases (leap years, year boundaries, etc.)
  • Integration tests

Run tests:

npm test

Run with coverage:

npm run test:coverage

TypeScript Support

This library is written in TypeScript and includes full type definitions.

import { dateToUnixTime, DateTimeInput } from 'time-to-unix-converter';

// TypeScript will provide full autocomplete and type checking
const timestamp: number = dateToUnixTime(2024, 12, 4, 14, 30, 0, '+05:30');

const input: DateTimeInput = {
  year: 2024,
  month: 12,
  day: 4,
  hour: 14,
  minute: 30,
  second: 0,
  timezone: '+05:30'
};

Performance

  • Fast: No external dependencies, uses native JavaScript Date API
  • Efficient: Minimal memory footprint
  • Optimized: Separate handling for UTC offsets vs IANA timezones

Security

  • All inputs are validated before processing
  • No external network calls
  • No eval() or dynamic code execution
  • Pure functions with no side effects
  • Safe for use in security-sensitive applications

Browser Compatibility

Works in all modern browsers and Node.js environments that support:

  • ES2020
  • Intl.DateTimeFormat (for IANA timezone support)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License - see LICENSE file for details

Author

Per Diem Website: tryperdiem.com

Support

Changelog

1.0.2

  • Added useMilliseconds parameter to return timestamps in milliseconds instead of seconds
  • Updated package name to time-to-unix-converter

1.0.0 (Initial Release)

  • Core functionality for converting date/time to Unix timestamps
  • Support for UTC offset and IANA timezone formats
  • Comprehensive input validation
  • Full TypeScript support
  • 100% test coverage

Made with ❤️ by the Per Diem team# date-with-timezone-to-unix