4.4.13 • Published 5 months ago

@conectate/ct-date v4.4.13

Weekly downloads
21
License
BSD-3-Clause
Repository
github
Last release
5 months ago

Published on webcomponents.org

ct-date

A simple cross-platform date input component with optional time selection.

Installation

# npm
npm i @conectate/ct-date

# yarn
yarn add @conectate/ct-date

# pnpm
pnpm i @conectate/ct-date

Usage

Basic Usage

import '@conectate/ct-date';

// Then use in your HTML
<ct-date label="Select Date"></ct-date>

Examples

IMPORTANT: ct-date by default shows UTC date, if you want to show local date, you can use the usetimezone attribute.

<!-- Basic date input -->
<ct-date label="Date"></ct-date>

<!-- Date input with time selection -->
<ct-date label="Date and Time" showhour></ct-date>

<!-- Date input without day selection (month and year only) -->
<ct-date label="Month and Year" nodd></ct-date>

<!-- Required date with validation -->
<ct-date label="Required Date" required></ct-date>

<!-- With min/max year limitations -->
<ct-date label="Limited Year Range" minYYYY="2000" maxYYYY="2030"></ct-date>

<!-- With timezone awareness -->
<ct-date label="Local Timezone" usetimezone></ct-date>

Handling Date Selection

// Listen for date selection events
const dateInput = document.querySelector('ct-date');
dateInput.addEventListener('value', (e) => {
  // e.detail contains Unix timestamp in seconds
  console.log('Selected date timestamp:', e.detail);

  // Convert to JavaScript Date object if needed
  const date = new Date(e.detail * 1000);
  console.log('Selected date:', date);
});

// Set date programmatically (using Unix timestamp in seconds)
dateInput.value = 1646137200; // March 1, 2022 UTC

// Validate the input
const isValid = dateInput.validate();
console.log('Is valid date:', isValid);

// Get the current value
const timestamp = dateInput.value;
console.log('Current timestamp:', timestamp);

// Get formatted date value
const formattedDate = dateInput.valueFormat;
console.log('Formatted date:', formattedDate); // e.g. "2022-03-01" or "2022-03-01T12:30:00Z"

Paste Support

The component supports pasting date strings in various formats:

  • DD/MM/YYYY
  • YYYY-MM-DD
  • DD/MM/YYYY HH:MM
  • YYYY-MM-DD HH:MM

Simply paste the date string into the input field, and it will automatically parse and fill the appropriate fields.

API

Properties

PropertyAttributeTypeDefaultDescription
labellabelstring""Label text for the input
placeholderplaceholderstring""Placeholder text for the input
noddnoddbooleanfalseHides the day input field when true
showhourshowhourbooleanfalseShows hour/minute input fields when true
usetimezoneusetimezonebooleanfalseUses local timezone instead of UTC
requiredrequiredbooleanfalseMarks the input as required
hiddenhiddenbooleanfalseHides the input
invalidinvalidbooleanfalseIndicates validation state
minYYYYminYYYYnumber1800Minimum allowed year
maxYYYYmaxYYYYnumber2300Maximum allowed year
value-number-Timestamp value in seconds (getter/setter)
valueFormat-string-Formatted date string (getter)

Methods

NameDescriptionParametersReturns
validate()Validates the input-boolean
loadValue()Loads a date valuevalue: number\|string\|undefined-
plainTextToDate()Parses a date stringdata?: string-

Events

Event NameDetailDescription
valueUnix timestamp in secondsFires when a valid date is selected

Styling

The component uses ct-input-container and inherits its styling options. It can be customized with standard CSS.

Follow me

Herberth Obregón

https://x.com/herberthobregon

https://dev.to/herberthobregon

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

History

  • v1.0.0 Initial Release

License

See LICENSE

4.4.3

8 months ago

4.4.13

5 months ago

4.4.12

6 months ago

4.4.9

6 months ago

4.4.5

8 months ago

4.4.4

8 months ago

4.3.4

8 months ago

4.3.0

9 months ago

4.2.5

1 year ago

4.1.5

1 year ago

4.1.0

1 year ago

4.0.0

1 year ago

4.2.1

1 year ago

4.1.2

1 year ago

4.2.0

1 year ago

3.13.1

2 years ago

3.13.0

2 years ago

3.12.9

2 years ago

3.9.2

2 years ago

3.9.1

2 years ago

3.8.2

2 years ago

3.9.0

2 years ago

3.8.1

2 years ago

3.9.5

2 years ago

3.11.0

2 years ago

3.8.0

2 years ago

3.10.0

2 years ago

3.12.1

2 years ago

3.12.0

2 years ago

3.11.1

2 years ago

3.7.7

2 years ago

3.7.5

3 years ago

3.7.4

3 years ago

3.7.3

3 years ago

3.7.1

3 years ago

3.7.0

3 years ago

3.6.0

4 years ago

3.5.13

4 years ago

3.5.11

4 years ago

3.5.5

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

5 years ago

3.0.0

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago