1

What I'd like to achieve should look like this in the bottom corner. However, I'd like to set a timezone, e.g. New York (that's not a timezone I know lol, but you understand) and it should display 'New York April 1st 05:42'.

mxainz
  • 11
  • 1
  • Can you please show us what you've tried? – briosheje Apr 01 '19 at 09:54
  • Use https://momentjs.com/. – Nicolae Maties Apr 01 '19 at 09:55
  • Please avoid asking multiple questions in one post. Also, you should make **some** attempt at writing your own code. StackOverflow is not a free code writing service. – RobG Apr 01 '19 at 10:13
  • @RobG I'm 17 and have 0 experience with Javascript. I know Stackoverflow is not a free code writing service but I can't just learn it in a day and I'd like to have it for school. I was hoping there'd be someone nice to help me out. – mxainz Apr 01 '19 at 10:24
  • @mxainz You can use moment.js as Nicolae mentioned to set timezones otherwise I dont think you can set timezone in plain javascript Date object – Andam Apr 01 '19 at 13:22

2 Answers2

1

Use this

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

setInterval(function(){ 
    var now = new Date();
    var time = monthNames[now.getMonth()] + " " + now.getDate() + ", " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
    document.getElementById("currentTime").innerHTML = time; 
  }, 1000);
  
document.getElementById("name").innerHTML = Intl.DateTimeFormat().resolvedOptions().timeZone + ", ";
<p id="time"><span id="name"></span> <span id="currentTime"></span></p>
Andam
  • 2,087
  • 1
  • 8
  • 21
1

// get a new date (locale machine date time)
var date = new Date();

// First converting to UTC and then adding +360 to make it +5 GMT 
const millisecondsOffset = ((date.getTimezoneOffset() + 360 ) * 60 * 1000);
date.setTime(date.getTime() - millisecondsOffset);

// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();

// find the html element with the id of time
// set the innerHTML of that element to the date a space the time
document.getElementById('time').innerHTML = 'Islamabad, Karachi'+ ' ' + n + ' ' + time;
<div id='time'></div>
Hiteshdua1
  • 2,126
  • 18
  • 29