Convert current time to milliseconds using moment
Suppose I have this time
'2018-08-03T15:53:57.000Z'
I need to convert only the time part to milliseconds
I tried this but didn't work and throws error
moment.utc("2018-08-03T15:53:57.000Z").format('HH:mm:ss').milliseconds()
Error
TypeError: (0 , _moment2.default)(...).format(...).milliseconds is not a function
Can someone please help how can I convert only time to millisecond?
Thank you!!!
javascript momentjs
|
show 8 more comments
Suppose I have this time
'2018-08-03T15:53:57.000Z'
I need to convert only the time part to milliseconds
I tried this but didn't work and throws error
moment.utc("2018-08-03T15:53:57.000Z").format('HH:mm:ss').milliseconds()
Error
TypeError: (0 , _moment2.default)(...).format(...).milliseconds is not a function
Can someone please help how can I convert only time to millisecond?
Thank you!!!
javascript momentjs
What error does it throw?
– William Chong
Nov 22 '18 at 10:21
@WilliamChong Updated the question
– Profer
Nov 22 '18 at 10:23
Welcome to Stack Overflow. Would you like milliseconds from 00:00 local user time to 15:53:57.000 UTC or 00:00 to 15:53:57.000 local user time?
– HMR
Nov 22 '18 at 10:24
1
new Date("2018-08-03T15:53:57.000Z").getTime()
– Mukesh Verma
Nov 22 '18 at 10:24
1
So theZ
in15:53:57.000Z
is not used at all? If you donew Date('2018-08-03T15:53:57.000Z')
you'll likely get a different time unless you happen to be in the UK.
– HMR
Nov 22 '18 at 10:29
|
show 8 more comments
Suppose I have this time
'2018-08-03T15:53:57.000Z'
I need to convert only the time part to milliseconds
I tried this but didn't work and throws error
moment.utc("2018-08-03T15:53:57.000Z").format('HH:mm:ss').milliseconds()
Error
TypeError: (0 , _moment2.default)(...).format(...).milliseconds is not a function
Can someone please help how can I convert only time to millisecond?
Thank you!!!
javascript momentjs
Suppose I have this time
'2018-08-03T15:53:57.000Z'
I need to convert only the time part to milliseconds
I tried this but didn't work and throws error
moment.utc("2018-08-03T15:53:57.000Z").format('HH:mm:ss').milliseconds()
Error
TypeError: (0 , _moment2.default)(...).format(...).milliseconds is not a function
Can someone please help how can I convert only time to millisecond?
Thank you!!!
javascript momentjs
javascript momentjs
edited Nov 22 '18 at 10:23
Profer
asked Nov 22 '18 at 10:20
ProferProfer
61112
61112
What error does it throw?
– William Chong
Nov 22 '18 at 10:21
@WilliamChong Updated the question
– Profer
Nov 22 '18 at 10:23
Welcome to Stack Overflow. Would you like milliseconds from 00:00 local user time to 15:53:57.000 UTC or 00:00 to 15:53:57.000 local user time?
– HMR
Nov 22 '18 at 10:24
1
new Date("2018-08-03T15:53:57.000Z").getTime()
– Mukesh Verma
Nov 22 '18 at 10:24
1
So theZ
in15:53:57.000Z
is not used at all? If you donew Date('2018-08-03T15:53:57.000Z')
you'll likely get a different time unless you happen to be in the UK.
– HMR
Nov 22 '18 at 10:29
|
show 8 more comments
What error does it throw?
– William Chong
Nov 22 '18 at 10:21
@WilliamChong Updated the question
– Profer
Nov 22 '18 at 10:23
Welcome to Stack Overflow. Would you like milliseconds from 00:00 local user time to 15:53:57.000 UTC or 00:00 to 15:53:57.000 local user time?
– HMR
Nov 22 '18 at 10:24
1
new Date("2018-08-03T15:53:57.000Z").getTime()
– Mukesh Verma
Nov 22 '18 at 10:24
1
So theZ
in15:53:57.000Z
is not used at all? If you donew Date('2018-08-03T15:53:57.000Z')
you'll likely get a different time unless you happen to be in the UK.
– HMR
Nov 22 '18 at 10:29
What error does it throw?
– William Chong
Nov 22 '18 at 10:21
What error does it throw?
– William Chong
Nov 22 '18 at 10:21
@WilliamChong Updated the question
– Profer
Nov 22 '18 at 10:23
@WilliamChong Updated the question
– Profer
Nov 22 '18 at 10:23
Welcome to Stack Overflow. Would you like milliseconds from 00:00 local user time to 15:53:57.000 UTC or 00:00 to 15:53:57.000 local user time?
– HMR
Nov 22 '18 at 10:24
Welcome to Stack Overflow. Would you like milliseconds from 00:00 local user time to 15:53:57.000 UTC or 00:00 to 15:53:57.000 local user time?
– HMR
Nov 22 '18 at 10:24
1
1
new Date("2018-08-03T15:53:57.000Z").getTime()
– Mukesh Verma
Nov 22 '18 at 10:24
new Date("2018-08-03T15:53:57.000Z").getTime()
– Mukesh Verma
Nov 22 '18 at 10:24
1
1
So the
Z
in 15:53:57.000Z
is not used at all? If you do new Date('2018-08-03T15:53:57.000Z')
you'll likely get a different time unless you happen to be in the UK.– HMR
Nov 22 '18 at 10:29
So the
Z
in 15:53:57.000Z
is not used at all? If you do new Date('2018-08-03T15:53:57.000Z')
you'll likely get a different time unless you happen to be in the UK.– HMR
Nov 22 '18 at 10:29
|
show 8 more comments
4 Answers
4
active
oldest
votes
No need to use moment to do this. The date string can be parsed sufficiently by vanilla JS.
var date = new Date('2018-08-03T15:53:57.000Z');
And to get the timestamp (in milliseconds) of this date;
var millis = date.getTime();
And, since there are 86400 seconds in a day (24*60*60) there are 86,400,000 milliseconds and we can use the remainder after divission by this number to get the number of milliseconds the time portion represents.
var millisToday = millis % 8640000;
UPDATE
Now using getTime()
instead of valueOf()
as it is the "proper" way to get the timestamp of the Date object.
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
@HMR Would like to elaborate as to why? How is it different tovalueOf()
?
– phuzi
Nov 22 '18 at 10:43
1
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
For more info; here is an example of type coercion:1 - {valueOf:()=>1}
is 0 where1 - {valueOf:()=>2}
is -1
– HMR
Nov 22 '18 at 11:03
add a comment |
Simply do this if you want to get the milliseconds:
const ms = moment.utc("2018-08-03T15:53:57.000Z").valueOf()
add a comment |
I'll add my answer in case I got it wrong then someone can comment.
If I send new Date().toISOString()
to someone in a different time zone then the time will differ for this person. If I ask that person to have a skype call at 13:00 their time it could mean it's 18:00 my local time.
So if the person sending me the date string is from the UK and sends me ...T13:00.000Z
That actually means 18:00 for me.
Here is how you can correctly get the time in milliseconds from your midnight of the date converted to your local time:
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
Example where DST goes in effect:
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then2018-03-25T02:00:00+01:00
but midnight was2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!
– phuzi
Nov 22 '18 at 11:35
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
What DST refers to?
– Profer
Nov 23 '18 at 10:39
|
show 5 more comments
The function is moment().milliseconds()
Not format().milliseconds
add a comment |
Your Answer
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%2f53428720%2fconvert-current-time-to-milliseconds-using-moment%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
No need to use moment to do this. The date string can be parsed sufficiently by vanilla JS.
var date = new Date('2018-08-03T15:53:57.000Z');
And to get the timestamp (in milliseconds) of this date;
var millis = date.getTime();
And, since there are 86400 seconds in a day (24*60*60) there are 86,400,000 milliseconds and we can use the remainder after divission by this number to get the number of milliseconds the time portion represents.
var millisToday = millis % 8640000;
UPDATE
Now using getTime()
instead of valueOf()
as it is the "proper" way to get the timestamp of the Date object.
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
@HMR Would like to elaborate as to why? How is it different tovalueOf()
?
– phuzi
Nov 22 '18 at 10:43
1
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
For more info; here is an example of type coercion:1 - {valueOf:()=>1}
is 0 where1 - {valueOf:()=>2}
is -1
– HMR
Nov 22 '18 at 11:03
add a comment |
No need to use moment to do this. The date string can be parsed sufficiently by vanilla JS.
var date = new Date('2018-08-03T15:53:57.000Z');
And to get the timestamp (in milliseconds) of this date;
var millis = date.getTime();
And, since there are 86400 seconds in a day (24*60*60) there are 86,400,000 milliseconds and we can use the remainder after divission by this number to get the number of milliseconds the time portion represents.
var millisToday = millis % 8640000;
UPDATE
Now using getTime()
instead of valueOf()
as it is the "proper" way to get the timestamp of the Date object.
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
@HMR Would like to elaborate as to why? How is it different tovalueOf()
?
– phuzi
Nov 22 '18 at 10:43
1
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
For more info; here is an example of type coercion:1 - {valueOf:()=>1}
is 0 where1 - {valueOf:()=>2}
is -1
– HMR
Nov 22 '18 at 11:03
add a comment |
No need to use moment to do this. The date string can be parsed sufficiently by vanilla JS.
var date = new Date('2018-08-03T15:53:57.000Z');
And to get the timestamp (in milliseconds) of this date;
var millis = date.getTime();
And, since there are 86400 seconds in a day (24*60*60) there are 86,400,000 milliseconds and we can use the remainder after divission by this number to get the number of milliseconds the time portion represents.
var millisToday = millis % 8640000;
UPDATE
Now using getTime()
instead of valueOf()
as it is the "proper" way to get the timestamp of the Date object.
No need to use moment to do this. The date string can be parsed sufficiently by vanilla JS.
var date = new Date('2018-08-03T15:53:57.000Z');
And to get the timestamp (in milliseconds) of this date;
var millis = date.getTime();
And, since there are 86400 seconds in a day (24*60*60) there are 86,400,000 milliseconds and we can use the remainder after divission by this number to get the number of milliseconds the time portion represents.
var millisToday = millis % 8640000;
UPDATE
Now using getTime()
instead of valueOf()
as it is the "proper" way to get the timestamp of the Date object.
edited Nov 22 '18 at 11:00
answered Nov 22 '18 at 10:33


phuziphuzi
4,73012035
4,73012035
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
@HMR Would like to elaborate as to why? How is it different tovalueOf()
?
– phuzi
Nov 22 '18 at 10:43
1
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
For more info; here is an example of type coercion:1 - {valueOf:()=>1}
is 0 where1 - {valueOf:()=>2}
is -1
– HMR
Nov 22 '18 at 11:03
add a comment |
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
@HMR Would like to elaborate as to why? How is it different tovalueOf()
?
– phuzi
Nov 22 '18 at 10:43
1
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
For more info; here is an example of type coercion:1 - {valueOf:()=>1}
is 0 where1 - {valueOf:()=>2}
is -1
– HMR
Nov 22 '18 at 11:03
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
I'd recommend using getTime instead.
– HMR
Nov 22 '18 at 10:41
@HMR Would like to elaborate as to why? How is it different to
valueOf()
?– phuzi
Nov 22 '18 at 10:43
@HMR Would like to elaborate as to why? How is it different to
valueOf()
?– phuzi
Nov 22 '18 at 10:43
1
1
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
getTime was explicitly created to get milliseconds after epoch where valueOf is a general function used in type coercion.
– HMR
Nov 22 '18 at 10:47
For more info; here is an example of type coercion:
1 - {valueOf:()=>1}
is 0 where 1 - {valueOf:()=>2}
is -1– HMR
Nov 22 '18 at 11:03
For more info; here is an example of type coercion:
1 - {valueOf:()=>1}
is 0 where 1 - {valueOf:()=>2}
is -1– HMR
Nov 22 '18 at 11:03
add a comment |
Simply do this if you want to get the milliseconds:
const ms = moment.utc("2018-08-03T15:53:57.000Z").valueOf()
add a comment |
Simply do this if you want to get the milliseconds:
const ms = moment.utc("2018-08-03T15:53:57.000Z").valueOf()
add a comment |
Simply do this if you want to get the milliseconds:
const ms = moment.utc("2018-08-03T15:53:57.000Z").valueOf()
Simply do this if you want to get the milliseconds:
const ms = moment.utc("2018-08-03T15:53:57.000Z").valueOf()
answered Nov 22 '18 at 10:30


John KennedyJohn Kennedy
2,69821126
2,69821126
add a comment |
add a comment |
I'll add my answer in case I got it wrong then someone can comment.
If I send new Date().toISOString()
to someone in a different time zone then the time will differ for this person. If I ask that person to have a skype call at 13:00 their time it could mean it's 18:00 my local time.
So if the person sending me the date string is from the UK and sends me ...T13:00.000Z
That actually means 18:00 for me.
Here is how you can correctly get the time in milliseconds from your midnight of the date converted to your local time:
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
Example where DST goes in effect:
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then2018-03-25T02:00:00+01:00
but midnight was2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!
– phuzi
Nov 22 '18 at 11:35
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
What DST refers to?
– Profer
Nov 23 '18 at 10:39
|
show 5 more comments
I'll add my answer in case I got it wrong then someone can comment.
If I send new Date().toISOString()
to someone in a different time zone then the time will differ for this person. If I ask that person to have a skype call at 13:00 their time it could mean it's 18:00 my local time.
So if the person sending me the date string is from the UK and sends me ...T13:00.000Z
That actually means 18:00 for me.
Here is how you can correctly get the time in milliseconds from your midnight of the date converted to your local time:
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
Example where DST goes in effect:
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then2018-03-25T02:00:00+01:00
but midnight was2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!
– phuzi
Nov 22 '18 at 11:35
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
What DST refers to?
– Profer
Nov 23 '18 at 10:39
|
show 5 more comments
I'll add my answer in case I got it wrong then someone can comment.
If I send new Date().toISOString()
to someone in a different time zone then the time will differ for this person. If I ask that person to have a skype call at 13:00 their time it could mean it's 18:00 my local time.
So if the person sending me the date string is from the UK and sends me ...T13:00.000Z
That actually means 18:00 for me.
Here is how you can correctly get the time in milliseconds from your midnight of the date converted to your local time:
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
Example where DST goes in effect:
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
I'll add my answer in case I got it wrong then someone can comment.
If I send new Date().toISOString()
to someone in a different time zone then the time will differ for this person. If I ask that person to have a skype call at 13:00 their time it could mean it's 18:00 my local time.
So if the person sending me the date string is from the UK and sends me ...T13:00.000Z
That actually means 18:00 for me.
Here is how you can correctly get the time in milliseconds from your midnight of the date converted to your local time:
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
Example where DST goes in effect:
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
const date = new Date(2007, 1, 1, 0, 0, 2).toISOString();
console.log('date:',date);
console.log('date in your local time:',new Date(date).toString());
const millisecondsFromMidNight = (date) => {
var dateObject = new Date(date);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(millisecondsFromMidNight(date));
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
var minutesFromMidnight = (date) => {
var dateObject = new Date(date);
console.log('date:', date);
console.log(
'date in your local time:',
dateObject.toString(),
);
return (
dateObject.getTime() -
new Date(
dateObject.getFullYear(),
dateObject.getMonth(),
dateObject.getDate(),
0,
0,
0,
0,
).getTime()
);
};
console.log(
minutesFromMidnight('2018-10-28T00:59:00.000Z') / 60000,
);
console.log(
minutesFromMidnight('2018-10-28T01:01:00.000Z') / 60000,
);
edited Nov 22 '18 at 12:52
answered Nov 22 '18 at 11:25
HMRHMR
13.9k113899
13.9k113899
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then2018-03-25T02:00:00+01:00
but midnight was2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!
– phuzi
Nov 22 '18 at 11:35
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
What DST refers to?
– Profer
Nov 23 '18 at 10:39
|
show 5 more comments
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then2018-03-25T02:00:00+01:00
but midnight was2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!
– phuzi
Nov 22 '18 at 11:35
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
What DST refers to?
– Profer
Nov 23 '18 at 10:39
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then
2018-03-25T02:00:00+01:00
but midnight was 2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!– phuzi
Nov 22 '18 at 11:35
This will likely fail if there is a change in daylight savings on the date in question. e.g. in the UK, on 25 March 2018 at 01:00 daylight savings began so the time is then
2018-03-25T02:00:00+01:00
but midnight was 2018-03-25T00:00:00+00:00
. Although it is only one hour since midnight, i believe your answer will count 2. Don't know if that's a level of detail beyond what OP wants though!– phuzi
Nov 22 '18 at 11:35
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@phuzi I have added an example minutes from midnight with one value in DST and one 2 minutes later out of DST (western Europe) and it works correctly.
– HMR
Nov 22 '18 at 12:54
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@HMR Thank you for the help but I think phuzi's answer is correct
– Profer
Nov 23 '18 at 10:36
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
@Profer It is if you're in the UK and not currently in DST.
– HMR
Nov 23 '18 at 10:38
What DST refers to?
– Profer
Nov 23 '18 at 10:39
What DST refers to?
– Profer
Nov 23 '18 at 10:39
|
show 5 more comments
The function is moment().milliseconds()
Not format().milliseconds
add a comment |
The function is moment().milliseconds()
Not format().milliseconds
add a comment |
The function is moment().milliseconds()
Not format().milliseconds
The function is moment().milliseconds()
Not format().milliseconds
answered Nov 22 '18 at 10:34
Nick6707Nick6707
65
65
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%2f53428720%2fconvert-current-time-to-milliseconds-using-moment%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
What error does it throw?
– William Chong
Nov 22 '18 at 10:21
@WilliamChong Updated the question
– Profer
Nov 22 '18 at 10:23
Welcome to Stack Overflow. Would you like milliseconds from 00:00 local user time to 15:53:57.000 UTC or 00:00 to 15:53:57.000 local user time?
– HMR
Nov 22 '18 at 10:24
1
new Date("2018-08-03T15:53:57.000Z").getTime()
– Mukesh Verma
Nov 22 '18 at 10:24
1
So the
Z
in15:53:57.000Z
is not used at all? If you donew Date('2018-08-03T15:53:57.000Z')
you'll likely get a different time unless you happen to be in the UK.– HMR
Nov 22 '18 at 10:29