2

I want to display a UTC date using this JavaScriptcode on my webpage.

<script>
    function myDate()
    {
    var now = new Date();
    var d = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
    var x = document.getElementById("demo");
    x.innerHTML=d;
    }
</script>

With this code I am getting UTC date displayed as a local string as follows: "Thu Jul 04 2013 00:00:00 GMT+0530 (India Standard Time)"

I do not want display the string with a local time offset (GMT+0530 (IST)), instead I want the time to appear as UTC string format

dcarson
  • 2,853
  • 1
  • 25
  • 34
Sukhpreet
  • 117
  • 6

3 Answers3

2

The date returned by different browser are of different format

to remove GMT OFFSET from date you can use replace

var d = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
d = d.toString().replace(/GMT.+/,"");
Arun Killu
  • 13,581
  • 5
  • 34
  • 61
1

Firstly, the problem is that you are instantiating a local Date object by passing in the UTC year, month and day. This then creates a local Date with the values provided. by doing this you might be creating an incorrect date based on whether you want it to be UTC or local. IN your case, if you want var now as UTC, the way you are currently instantiating is incorrect as its in local time.

Anyway, dates can be tricky in in JavaScript, so I would consider using Moment.js for this

It's a fantastic library that provides all of the functions for manipulating and converting JavaScript dates that you could ever need.

For example with moment you can just do the following:

var now = moment(); // current date and time in local format
var nowAsUTC = now.utc(); // current local date and time converted to UTC
var alsoNowAsUTC = moment.utc() // same as the line above, but staring in UTC
console.log(nowUTC.format("DD/MM/YYYY, hh:mm:ss"))// prints a pretty UTC string
dcarson
  • 2,853
  • 1
  • 25
  • 34
  • Thanks for help! But i am still getting "Thu Jul 04 2013 03:22:20 GMT+0000" but i want to display UTC - 8 date only, e.g. "Thu Jul 04 2013" no time and GMT things etc – Sukhpreet Jul 04 '13 at 03:23
  • If you were to use moment and my example you would need to change the format string to: `nowUTC.format("ddd MMM DD YYYY")` or `now.format("ddd MMM DD YYYY")` – dcarson Jul 04 '13 at 03:45
0

Hmmm.. Are you sure you want to display UTC-8? I will take a guess that you are really wanting to convert the time to US Pacific time zone. That is not always UTC-8. Sometimes it is UTC-8, and sometimes it is UTC-7.

If you're not actually in the US Pacific Time zone, the only way to do this reliably in JavaScript is with a library that implements the TZDB database. I list several of them here.

For example, using walltime-js library, you can do the following:

var date = new Date();
var pacific = WallTime.UTCToWallTime(date, "America/Los_Angeles");
var s = pacific.toDateString() + ' ' + pacific.toFormattedTime();

// output:  "Fri Apr 26 2013 5:44 PM"

You can't just add or subtract a fixed number, because the target time zone may use a different offset depending on exactly what date you're talking about. This is primarily due to Daylight Saving Time, but also because time zones have changed over time.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575