Categories
discuss

What range of dates are permitted in Javascript?

What is the maximum and the minimum date that I can use with the Date object in Javascript?

Is it possible to represent ancient historical dates (like January 1, 2,500 B.C.) or dates that are far into the future (like October 7, 10,000)?

If these far-from-present dates can’t be represented with the Date object, how should I represent them?

Answer

According to §15.9.1.1 of the ECMA-262 specification,

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC.

The actual range of times supported by ECMAScript Date objects is … exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC.

So the earliest date representable with the Date object is fairly far beyond known human history:

new Date(-8640000000000000).toUTCString()
// Tue, 20 Apr 271,822 B.C. 00:00:00 UTC

The latest date is sufficient to last beyond Y10K and even beyond Y100K, but will need to be reworked a few hundred years before Y276K.

new Date(8640000000000000).toUTCString()
// Sat, 13 Sep 275,760 00:00:00 UTC

Any date outside of this range will return Invalid Date.

new Date(8640000000000001)   // Invalid Date
new Date(-8640000000000001)  // Invalid Date

In short, the JavaScript Date type will be sufficient for measuring time to millisecond precision within approximately 285,616 years before or after January 1, 1970. The dates posted in the question are very comfortably inside of this range.

Categories
discuss

Is && statement() the same as if() statement()?

Are the two identical?

Suppose you have:

var x = true;

And then you have one of either:

x && doSomething();

or

if(x) doSomething();

Is there any differene whatsoever between the two syntaxes? Did I stumble across a nice bit of sugar?

Answer

Strictly speaking, they will produce the same results, but if you use the former case as a condition for something else, you will get dissimilar results. This is because in the case of x && doSomething(), doSomething() will return a value to signify its success.

Categories
discuss

What is the criteria for throwing exceptions in subclass

What I have known till now is that a subclass if overriding a superclass method should throw the same exception or a subclass of the exception.

For example:

This is correct

class SuperClass {
    public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
 String signature = "(String, Integer[])";
 System.out.println(str + " " + signature);
 return 1;
 }
}

public final class SubClass extends SuperClass {
    public int doIt(String str, Integer... data) throws ArrayIndexOutOfBoundsException {
        String signature = "(String, Integer[])";
        System.out.println("Overridden: " + str + " " + signature);
        return 0;
    }

    public static void main(String... args) {
        SuperClass sb = new SubClass();
        try {
            sb.doIt("hello", 3);
        } catch (Exception e) {
        }
    }
}

This is Incorrect

class SuperClass {
    public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
 String signature = "(String, Integer[])";
 System.out.println(str + " " + signature);
 return 1;
 }
}

public final class SubClass extends SuperClass {
    public int doIt(String str, Integer... data) throws Exception {
        String signature = "(String, Integer[])";
        System.out.println("Overridden: " + str + " " + signature);
        return 0;
    }

    public static void main(String... args) {
        SuperClass sb = new SubClass();
        try {
            sb.doIt("hello", 3);
        } catch (Exception e) {
        }
    }
}

But my question is, why this code block is considered correct by compiler?

class SuperClass {
    public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
 String signature = "(String, Integer[])";
 System.out.println(str + " " + signature);
 return 1;
 }
}

public final class SubClass extends SuperClass {
    public int doIt(String str, Integer... data) throws RuntimeException {
        String signature = "(String, Integer[])";
        System.out.println("Overridden: " + str + " " + signature);
        return 0;
    }

    public static void main(String... args) {
        SuperClass sb = new SubClass();
        try {
            sb.doIt("hello", 3);
        } catch (Exception e) {
        }
    }
}

Answer

This is because in Java every method can throw a RuntimeException (or an Error) at any time. It does not even need to be declared in the throws part of your method signature. So it is possible to also throw a exception which is a super type of the one declared in your overridden method, as long it is still a sub type of RuntimeException.

See Chapter 11 (Exceptions) of the Java Language Specification for the specification for this behavior, especially 11.1.1. The Kinds of Exceptions which defines checked (needs to be specified in throws clause) and unchecked (does not need to be specified in throwsclause) exceptions.

Categories
discuss

Pass variable to RegExp with word boundary

I have to pass to RegExp value of variable and point a word boundary. I have a string to be checked if it contains a variable value. I don’t know how to pass to regexp as a variable value and a word boundary attribute.

So something like this:

var sa="Sample";
var re=new RegExp(/b/+sa);
alert(re.test("Sample text"));

I tried some ways to solve a problem but still can’t do that 🙁

Answer

Use this: re = new RegExp("\b" + sa)

And as @RobW mentioned, you may need to escape the sa.

See this: Is there a RegExp.escape function in Javascript?

Categories
discuss

Setting iFrame CSS to display:none during load

I have this:

<div class="upload_slide">
    <iframe class="upload_iframe" style="visibility:hidden;"></iframe>
</div>

If I then post some files to the iFrame and set .upload_slide to display:none;, while it’s loading, will this affect the loading of the iFrame or the detection of when it finished loading?

Answer

No, the iFrame’s loading and load detection should not be compromised by setting the display to none.

CSS is just for style, it has no ability to affect the DOM.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..