1

In my code, I have the following line:

var today = new Date();

My research tells me this should set today as the current date. But where does this current date come from? In one instance, I set an object up with this current date. Then my app wants this object to timeout after 30 days, so I changed the date on the tablet and re-entered the app (it closed and reopened), and yet today still gets the same value from Date(). Where does Date() get its value from? If I wanted to get a tablet specific time, how would I do that?

Kevin
  • 109
  • 12
  • 1
    In short? Date is a function that returns the result of the calculated milliseconds since 1 January 1970 00:00:00 in UTC that has been converted into a format that comes default or one that you specify. – Joshua Nov 16 '16 at 14:55
  • Read this https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date – Deep Nov 16 '16 at 14:56
  • @Crowes seconds or milliseconds? – Nicholas Siegmundt Nov 16 '16 at 14:57
  • @NicholasSiegmundt MDN Reference states milliseconds, corrected my other comment. – Joshua Nov 16 '16 at 14:57
  • That still doesn't answer my question - I understand it gives me the number of seconds since Jan 1 1970, but until _when_ is my question? It's not based on the tablet time, because I changed the tablet date/time and still got the same value... – Kevin Nov 16 '16 at 15:01
  • @Kevin Current date and time in the same timezone, I'd assume. – Joshua Nov 16 '16 at 15:18
  • Javascript implementations exist within a host environment. It's the host that provides the information from which the implementation determines the time value (i.e. milliseconds since the epoch). If you change the date or time of the host system, *new Date()* should return a date object that reflects the changed date or time. The language specification does not provide any more detail than that. – RobG Nov 17 '16 at 04:05

3 Answers3

1

When you call new Date, the value is get from the current computer/table browser time and store it in variable.

If you want to get value for specific date you shoud pass the date like that:

var today = new Date('2016-11-11') // will get the 11 November date
  • Where does it grab from though ('current browser time')? It's not the tablet date/time, because I got the same value for both calls even though I changed the tablet date/time... – Kevin Nov 16 '16 at 15:03
  • I think somehow variable today does not refresh the value(not call new Date) could you provide some more code. thanks –  Nov 16 '16 at 15:05
  • That's all that I do. I startup the app, then set a breakpoint on that variable (I see the current date). Then I change the tablet date/time to 2 months ahead of now, and restart the app. I follow the same sequence - startup the app, hit the breakpoint, and see the same value. It's not because of the variable not updating. – Kevin Nov 16 '16 at 15:09
1

Maybe this answer is useful to you too. The Date is taken from your current system.

Community
  • 1
  • 1
juliocb92
  • 96
  • 2
  • 12
  • Where does it grab from though ('current system')? It's not the tablet date/time, because I got the same value for both calls even though I changed the tablet date/time... – Kevin Nov 16 '16 at 15:02
  • I suppose it depends where your browser updates it's time from, but that's more than likely the system. – Joshua Nov 16 '16 at 15:19
  • The Date came from the current time you have set up in your computer/terminal. It's possible your device get date from internet but you can configure it too. – juliocb92 Nov 16 '16 at 15:20
1

I figured out my problem. Apparently, my tablet's date/time was being set back to the current value every time I tried to go into my app. I don't know why it did this, but when I disconnected from the internet I was able to see the correct 2 months in advance that I wanted.

Kevin
  • 109
  • 12
  • Probably because most devices are set to use a time server to set their internal date and time by default. – RobG Nov 17 '16 at 04:05