Javascript date format such as ISO but local

how do i format javascript date like ISO format but local time?

with myDate.toISOString()I get the time like: "2012-09-13T19: 12: 23.826Z"

but here, it's 22:13, so how do I include the time zone in the above format?


I finished doing ...

pad=function(e,t,n){n=n||"0",t=t||2;while((""+e).length<t)e=n+e;return e}
c = new Date()
c.getFullYear()+"-"+pad(c.getMonth()+1)+"-"+pad(c.getDate()-5)+"T"+c.toLocaleTimeString().replace(/\D/g,':')+"."+pad(c.getMilliseconds(),3)
+9
source share
4 answers

AFAIK You cannot format dates in JavaScript (without using external libraries). The best you can do is "format it yourself." I mean:

var date = new Date();
var year = date.getFullYear();
var month = date......


var ISOdate = year + "-" + month + "-" + .... ;

But there are some good libraries that let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");)

EDIT:

, Moment.js: http://momentjs.com/

+5

! Date, , t = new Date()

  • z = t.getTimezoneOffset() * 60 * 1000

  • tLocal = tz

  • Date

    tLocal = new Date(tLocal)

  • ISO

    iso = tLocal.toISOString()

  • iso = iso.slice(0, 19)

  • 'T'

    iso = iso.replace('T', ' ')

- ISO-ish, "2018-08-01 22:45:50" .

+6

, , .

: tz , t , .

function dateToISOLikeButLocal(date) {
    const offsetMs = date.getTimezoneOffset() * 60 * 1000;
    const msLocal =  date.getTime() - offsetMs;
    const dateLocal = new Date(msLocal);
    const iso = dateLocal.toISOString();
    const isoLocal = iso.slice(0, 19);
    return isoLocal;
}

, URL:

"2018-11-16T12:23:50"
+1

- -- : .

var s = new Date().toLocaleString('sq-AL');

so maybe you could use

var s = new Date().toLocaleString('sq-AL');
s = s.replace(' ','T');
s += "Z";

I hope this helps someone ...

0
source

All Articles