I've brought up this topic before if...else .. statement

Now my question is... i need to specified the time... where I know Java only can recognise "8" "9"... for hours ... and minutes from 1-60....

ok what if i need the action to be like this

Instead of

    if ( time < 10 )

I want it to be like this

    if ( time < 10.30 ) //meaning .. I want it to be 10.30am <--- is it possible.. if yes.. how i can do that ?

Thanks in advance :)

Recommended Answers

All 3 Replies

This is a bit bulky but is flexible, error tollerant and does the job:

function timeCompare(d, base_d) {
    /* fn timeCompare : compares two dates, both of which can be passed in one of two ways.
     * d: (Date or String) the Date to be tested - a js Date object or a timeString h:m[:s[:ms]].
     * base_d: (Date or String) (optional) the base Date against which d is tested  - a js Date object or a timeString h:m[:s[:ms]].
     * d and base_d both default to time NOW if missing or invalid.
     * Returns a js object with the following properties:
     * - d: the original d converted where necessary from timestring to a js Date object
     * - base_d: the original base_d converted where necessary from timestring to a js Date object
     * - before: (boolean) true if d < base_d, otherwise false.
     * - after:  (boolean) true if d > baseDate, otherwise false.
     * - equalTo: (boolean)  true if d == baseDate, otherwise false.

    function throwError(message) {
        //Throws a non-fatal js error
        setTimeout(function() {
            throw (message); 
        }, 0);
    function time2Date(timeStr) {
        //converts (String) hh:mm[:ss[:ms]] to milliseconds since midnight
        var tArr = (timeStr.split) ? timeStr.split(':') : [];
        if (tArr.length < 2) {
            throwError('fn timeIsBefore(): argument is not in the format hh:mm, hh:mm, hh:mm:ss or hh:mm:ss:ms');
        tArr[0] = Number(tArr[0]);//hours
        tArr[1] = Number(tArr[1]);//minutes
        tArr[2] = Number(tArr[2]);//seconds
        tArr[3] = Number(tArr[3]);//milliseconds
        if(isNaN(tArr[0]) || isNaN(tArr[1]) || isNaN(tArr[2]) || isNaN(tArr[3])){
            throwError('Warning: fn timeIsBefore('+timeStr+'): hours, minutes, seconds or milliseconds is not a number');
        var d = new Date();
        d.setHours(tArr[0] || 0);//default missing or invalid hours to zero
        d.setMinutes(tArr[1] || 0);//default missing or invalid mins to zero
        d.setSeconds(tArr[2] || 0);//default missing or invalid secs to zero
        d.setMilliseconds(tArr[3] || 0);//default missing or invalid ms to zero
        return d;
    base_d = (!base_d) ? new Date() : (typeof base_d === 'string') ? time2Date(base_d) : (base_d.getTime) ? base_d : new Date();
    d =      (!d)      ? base_d     : (typeof      d === 'string') ? time2Date(     d) : (     d.getTime) ?      d : base_d;

    return {
        date: d,
        baseDate: base_d,
        before:  d.getTime() <  base_d.getTime(),
        after:   d.getTime() >  base_d.getTime(),
        equalTo: d.getTime() == base_d.getTime()

As you will see in the comment block at the top of the function timeCompare(), it accepts one or two times/dates each expressed as a javascript Date object (of any date within Date range) or a time string in the format "h:m" or "h:m:s" or "h:m:s:ms" (assumed to be today).

The function returns an object with properties representing the two times/dates that were compared and booleans for the tests before, after and equalTo.

Working examples are provided here.

Of course you can...

if ( time < 10.30 ){console.log("it is")} else {console.log("it isn't")};
>>"it is"

if ( time < 10.30 ){console.log("it is")} else {console.log("it isn't")};
>>"it isn't"

thanks Mr Troy.. That solve my problem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.