Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide time standards from javascript

I want to display only Day, Date, Time. Now it displays:

enter image description here

Actually I want to display like:

enter image description here

How to remove --- "(India Standard Time)"

Used code below:

<html>

<head>
  <title>(Type a title for your page here)</title>
  <script type="text/javascript">
    function display_c() {
      var refresh = 1000; // Refresh rate in milli seconds
      mytime = setTimeout('display_ct()', refresh)
    }

    function display_ct() {
      var strcount
      var x = new Date()
      document.getElementById('ct').innerHTML = x;
      tt = display_c();
    }
  </script>
</head>

<body onload=display_ct();>
  <span id='ct'></span>

</body>

</html>
like image 848
vishnu Avatar asked Sep 10 '26 13:09

vishnu


2 Answers

Try with some Regex pattern /GMT(.*)/g .toString() used to convert as a string for replace GMT and after all.

function display_c() {
      var refresh = 1000; // Refresh rate in milli seconds
      mytime = setTimeout('display_ct()', refresh)
    }

    function display_ct() {
      var strcount
      var x = new Date()
      document.getElementById('ct').innerHTML = x.toString().replace(/GMT(.*)/g,"");
      tt = display_c();
    }
display_ct()
  <span id='ct'></span>
like image 95
prasanth Avatar answered Sep 12 '26 01:09

prasanth


Another thing you can do is to use javascript's toLocaleString() method for more flexibility-

  1. It has options to format the string that you can specify - for instance in the demo below I'm using:

    var options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false};
    
  2. Now you can display the date string using this:

    document.getElementById('ct').innerHTML = x.toLocaleString('en-US', options);
    

See demo below:

<html>

<head>
  <title>(Type a title for your page here)</title>
  <script type="text/javascript">
    var options = { weekday: 'short', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: false};
    function display_c() {
      var refresh = 1000; // Refresh rate in milli seconds
      mytime = setTimeout('display_ct()', refresh)
    }

    function display_ct() {
      var strcount;
      var x = new Date();
      document.getElementById('ct').innerHTML = x.toLocaleString('en-US', options);
      tt = display_c();
    }
  </script>
</head>

<body onload=display_ct();>
  <span id='ct'></span>

</body>

</html>
like image 39
kukkuz Avatar answered Sep 12 '26 02:09

kukkuz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!