Remove local timezone from date in javascript?

I was wondering if I can remove the timezone from a date using javascript? I am getting datetime in json format, now I want to set it back, but it returns more information about what I really want / need.

Now I have:

var d = new Date();
d.setTime(1432851021000);
document.write(d);

      

and the output looks something like this:

Thu May 28 2015 16:10:21 GMT-0600 (CST)

      

Id like to only show up to the hour and remove GMT-0600 (CST).

I know javascript accepts this based on the user's current timezone, which is a problem because the information can be stored in different countries.

I am trying to avoid creating a format using something like:

d.date() + "/" + d.month()...etc

      

Is there a better solution for this?

+11


source to share


7 replies


I believe it d.toDateString()

will output the format you are looking for.

var d = new Date();
d.setTime(1432851021000);
d.toDateString(); // outputs to "Thu May 28 2015"
d.toGMTString(); //outputs to "Thu, 28 May 2015 22:10:21 GMT"

      



Or even d.toLocaleString()

"5/28/2015, 6:10:21 PM"

There are many methods for Date ()

+12


source


with impulses

var d = new Date();
d.setTime(1432851021000);

var newdate = moment(d);

alert(newdate.format("MMM DD/MM/YYYY HH:mm:ss "));

      



http://jsfiddle.net/vynvepak/

+2


source


You can use 2 options:
1. use split function: d.split ('GMT'). this gives you an array.
2. Get the usual js data functions like getHour and getFullYear etc. to format the result as you prefer.

0


source


Since the part you want is always of a fixed length you can use:

d.toString().slice(0, 24)

      

jsfiddle

0


source


var x = d.toDateString().length;
var result = d.toDateString() + d.toString().substring(x,x+9);

      

0


source


var date = new Date(myUnixTimestamp*1000);
var prettyDate = date.toLocaleString();

      

0


source


I created a simple js function to remove part of the timezone from a datetime string. This which will give output like

  • 2019-07-27T19: 00: 00.000-0700 => 2019-07-27T19: 00: 00.000
  • 2019-09-17T19: 00: 00.000 + 0100 => 2019-09-17T19: 00: 00.000

function removeTimeZonePart(dateString) {
            
             let finalDate = '';
            
             if (dateString.split('+').length > 1) {
                 let b = dateString.split('+');
            
                 finalDate = b[0];
             } else {
                 let b = dateString.split('-');
            
                  if (b.length > 1) {
                   b.pop();
                   finalDate = b.join('-');
                 }
             }

             return finalDate;

}

alert(removeTimeZonePart('2019-07-27T19:00:00.000-0700'));
      

Run codeHide result


0


source







All Articles