JavaScript

DatesetClock Method

Syntax

Date.setClock(time)

Arguments

timestring

The time to set on the date.

Description

Extension to the native date variable to allow for the quick setting of time on a date.

Discussion

The Date.setClock method takes a short string and parses a time out of it. It then sets the date to have the given time.

A ":" is used to separate hours, minutes and seconds, while a "." is used to separate seconds from milliseconds. "--" or "-" can be used to signify that the value at the given position should not be changed. "AM", "PM", "A" or "P" in both lower and upper case can be used to specify the value of a 12 hour clock.

Example

var d = new Date(2018,7,10,4,15,50,10);
d.setClock('--:15:30.500');
// d = Aug 10 2018 04:15:30.500
d.setClock('1 pm');
// d = Aug 10 2018 13:00:00.000