1

How can I get the current date, month and year in Javascript. I am using getFullYear() and getMonth() functions of JS's Date object. But these functions return the year and month of my PC. If I change my PC's date to an older or upcoming date, then these functions return those month and year.

How I can get current date, month and year, no matter what date user PC's shows?

Shyam
  • 782
  • 5
  • 12
  • JavaScript runs on the client's machine, thus this is the only time it can read out. If you wish to get the "actual" time (apart from generic Date/Time fun and timezones) you should rely on a third-party server to deliver you this time (via an XHR call maybe?). – Timusan Jan 08 '15 at 07:04

5 Answers5

4

Short of your code going out to a trusted source such as a known NTP server, you're going to be at the mercy of the local machine.

Even if you did attempt to go out to a trusted source, the local machine could subvert it by capturing your TCP traffic.

And, even if you created your own trusted machine which used encryption to deliver the correct date and time, a local machine could subvert that, simply by changing the JavaScript.

Bottom line, it's an arms race, and almost certainly a fools errand to think you can secure your code against everything. Perhaps it may be a good time to actually think why you need an accurate date in the first place.

If we knew that, we may be able to give advice outside the box you're currently in. Far too many questions of the form "How do I do X in a Y way?" discount a huge number of non-Y potential solutions :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Keeping paxdiablo's warnings in mind, some good resources [here](http://stackoverflow.com/questions/7802052/any-reference-to-free-internet-time-web-service). – Amadan Jan 08 '15 at 07:04
1

JavaScript does not work by magic. The only indication of the date it has is the date the machine it's being run on has.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Well i think you can do something like this

var ds = ... // Some ajax call to server 
var d = new Date(ds);

And after you have date you can easily get month and year part from date

Ancient
  • 3,007
  • 14
  • 55
  • 104
0

in short if you need only month and year you can construct datetime object at server level and make that as string and put in javascript from that variable you can again reconstruct javascript date object from that you can pull getyear and getmonth anything you need.

Ramakrishna
  • 4,928
  • 3
  • 20
  • 24
0

You did not mention your server side language but I solved it this way in Grails:

<script>
var serverDate=new Date(${new Date().getTime()});
//use serverDate
</script>

As you can see ${new Date().getTime()} is Grails code being executed at server side, so its value will be something like 1420700836298 (datetime) then var serverDate=new Date(1420700836298); will executed at client side;obviously serverDate will be date object according to the server time.

So your task will be to replace ${new Date().getTime()} with your server side lang.

N.B This is not ajax call, it will execute only once ; when the page loads for the first time.

t31321
  • 763
  • 1
  • 7
  • 22