Javascript date format like ISO but local
how do I format a javascript date like ISO format, but in local time?
with myDate.toISOString()
I am getting the time as: "2012-09-13T19:12:23.826Z"
but here, it is 22:13, so how do I include the timezone in above format?
I ended up 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)
javascript
add a comment |
how do I format a javascript date like ISO format, but in local time?
with myDate.toISOString()
I am getting the time as: "2012-09-13T19:12:23.826Z"
but here, it is 22:13, so how do I include the timezone in above format?
I ended up 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)
javascript
add a comment |
how do I format a javascript date like ISO format, but in local time?
with myDate.toISOString()
I am getting the time as: "2012-09-13T19:12:23.826Z"
but here, it is 22:13, so how do I include the timezone in above format?
I ended up 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)
javascript
how do I format a javascript date like ISO format, but in local time?
with myDate.toISOString()
I am getting the time as: "2012-09-13T19:12:23.826Z"
but here, it is 22:13, so how do I include the timezone in above format?
I ended up 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)
javascript
javascript
edited Sep 13 '12 at 20:11
Billy Moon
asked Sep 13 '12 at 19:15
Billy MoonBilly Moon
41.3k18100198
41.3k18100198
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
AFAIK you can't format dates in javascript (without using external libraries). The best you could 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 will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");
)
EDIT:
Moment.js seems the thing you're looking for: http://momentjs.com/
and the month returned by.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!
– Billy Moon
Sep 13 '12 at 19:33
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
add a comment |
No library required! For some Date
object, e.g. t = new Date()
convert the local time zone offset from minutes to milliseconds
z = t.getTimezoneOffset() * 60 * 1000
subtract the offset from t
tLocal = t-z
create shifted Date object
tLocal = new Date(tLocal)
convert to ISO format string
iso = tLocal.toISOString()
drop the milliseconds and zone
iso = iso.slice(0, 19)
replace the ugly 'T' with a space
iso = iso.replace('T', ' ')
Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.
add a comment |
I went with what Denis Howe said, below as a ready made function for convenience.
Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.
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;
}
With this I get the kind of string that needed as a URL parameter:
"2018-11-16T12:23:50"
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12413243%2fjavascript-date-format-like-iso-but-local%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
AFAIK you can't format dates in javascript (without using external libraries). The best you could 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 will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");
)
EDIT:
Moment.js seems the thing you're looking for: http://momentjs.com/
and the month returned by.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!
– Billy Moon
Sep 13 '12 at 19:33
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
add a comment |
AFAIK you can't format dates in javascript (without using external libraries). The best you could 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 will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");
)
EDIT:
Moment.js seems the thing you're looking for: http://momentjs.com/
and the month returned by.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!
– Billy Moon
Sep 13 '12 at 19:33
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
add a comment |
AFAIK you can't format dates in javascript (without using external libraries). The best you could 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 will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");
)
EDIT:
Moment.js seems the thing you're looking for: http://momentjs.com/
AFAIK you can't format dates in javascript (without using external libraries). The best you could 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 will let you format dates! (read "format" as in library.getDate("YYYY-MM-DD.........");
)
EDIT:
Moment.js seems the thing you're looking for: http://momentjs.com/
answered Sep 13 '12 at 19:21
alexandernstalexandernst
4,9271351124
4,9271351124
and the month returned by.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!
– Billy Moon
Sep 13 '12 at 19:33
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
add a comment |
and the month returned by.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!
– Billy Moon
Sep 13 '12 at 19:33
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
and the month returned by
.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!– Billy Moon
Sep 13 '12 at 19:33
and the month returned by
.getMonth()
si not padded, so I guess I would need a library to pad it too - seems like a lot of a faff!– Billy Moon
Sep 13 '12 at 19:33
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
Yes, indeed. It's a lot of work, that's why I'd recommend using Moment.js or some other library.
– alexandernst
Sep 13 '12 at 19:36
add a comment |
No library required! For some Date
object, e.g. t = new Date()
convert the local time zone offset from minutes to milliseconds
z = t.getTimezoneOffset() * 60 * 1000
subtract the offset from t
tLocal = t-z
create shifted Date object
tLocal = new Date(tLocal)
convert to ISO format string
iso = tLocal.toISOString()
drop the milliseconds and zone
iso = iso.slice(0, 19)
replace the ugly 'T' with a space
iso = iso.replace('T', ' ')
Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.
add a comment |
No library required! For some Date
object, e.g. t = new Date()
convert the local time zone offset from minutes to milliseconds
z = t.getTimezoneOffset() * 60 * 1000
subtract the offset from t
tLocal = t-z
create shifted Date object
tLocal = new Date(tLocal)
convert to ISO format string
iso = tLocal.toISOString()
drop the milliseconds and zone
iso = iso.slice(0, 19)
replace the ugly 'T' with a space
iso = iso.replace('T', ' ')
Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.
add a comment |
No library required! For some Date
object, e.g. t = new Date()
convert the local time zone offset from minutes to milliseconds
z = t.getTimezoneOffset() * 60 * 1000
subtract the offset from t
tLocal = t-z
create shifted Date object
tLocal = new Date(tLocal)
convert to ISO format string
iso = tLocal.toISOString()
drop the milliseconds and zone
iso = iso.slice(0, 19)
replace the ugly 'T' with a space
iso = iso.replace('T', ' ')
Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.
No library required! For some Date
object, e.g. t = new Date()
convert the local time zone offset from minutes to milliseconds
z = t.getTimezoneOffset() * 60 * 1000
subtract the offset from t
tLocal = t-z
create shifted Date object
tLocal = new Date(tLocal)
convert to ISO format string
iso = tLocal.toISOString()
drop the milliseconds and zone
iso = iso.slice(0, 19)
replace the ugly 'T' with a space
iso = iso.replace('T', ' ')
Result is a nice ISO-ish format date-time string like "2018-08-01 22:45:50" in the local time zone.
answered Aug 1 '18 at 23:59
Denis HoweDenis Howe
772913
772913
add a comment |
add a comment |
I went with what Denis Howe said, below as a ready made function for convenience.
Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.
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;
}
With this I get the kind of string that needed as a URL parameter:
"2018-11-16T12:23:50"
add a comment |
I went with what Denis Howe said, below as a ready made function for convenience.
Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.
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;
}
With this I get the kind of string that needed as a URL parameter:
"2018-11-16T12:23:50"
add a comment |
I went with what Denis Howe said, below as a ready made function for convenience.
Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.
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;
}
With this I get the kind of string that needed as a URL parameter:
"2018-11-16T12:23:50"
I went with what Denis Howe said, below as a ready made function for convenience.
Also one fix: in the original answer t-z does not work because t is a Date, not milliseconds.
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;
}
With this I get the kind of string that needed as a URL parameter:
"2018-11-16T12:23:50"
answered Nov 16 '18 at 10:25
antontantont
1,4021016
1,4021016
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12413243%2fjavascript-date-format-like-iso-but-local%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown