0

Hi I am trying to get current indian date and time. when I use function new Date() function

I get local system time is there any way to get real indian standard time not local time here is my code

var currentDa = new Date();
var nowTime = new Date(currentDa.getTime());
var Chour = nowTime.getHours();
var Cminute = nowTime.getMinutes();
var currentTime = (+Chour + "." + Cminute);
Himanshu Gupta
  • 69
  • 1
  • 10

2 Answers2

4

Javascript code is

d = new Date();
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
nd = new Date(utc + (3600000*+5.5));
var ist =  nd.toLocaleString();
console.log("IST now is : " +ist);
Jay
  • 9,189
  • 12
  • 56
  • 96
  • I tried this code in UK and I got IST time correctly... Are you trying this from India ? – Jay Sep 21 '15 at 06:45
  • 1
    yes i am trying this from india when i switch my system date to 23sept it shows 23sept and time not today's exact time – Himanshu Gupta Sep 21 '15 at 06:55
  • For this reason I provide you api solution time function call internally system time and if some way you spoof local time then your function will also return wrong (which is logically not wrong) time – Ajit kohir Sep 21 '15 at 07:12
  • 1
    @HimanshuGupta Please un-accept my answer if it does not solve the issue. – Jay Sep 21 '15 at 09:46
  • Hey I want it in Date datatype. I mean the one which is returned by new Date().... is there any way I can convert that ist into date format ? – Ashutosh Tiwari Jun 17 '21 at 14:13
0

You are correct the Date function of javascript return your system time. Actually there was link which used to work before JSON time service "json-time.appspot.com" is broken. Anyone have a replacement?

the link is broken but if you can setup the environment the guy mention in github it is good (personally i never tried )

the best what i have done in this case is use accuweather time API: https://apidev.accuweather.com/developers/timeZones

going to use for personal that fine for commercial check this link:

Is the AccuWeather API free for commercial use?

I believe this solve your problem

Community
  • 1
  • 1
Ajit kohir
  • 481
  • 5
  • 14