Jump to content

Problem in getUTCMilliseconds() and getMilliseconds()


Recommended Posts

I wanna return the milliseconds from a specified date and time...../// I've tried both in local time and UTC time ! but the following code shows "NaN"....why ?If the JS is wrong, then what should be the correct ?

var d = new Date("July 21, 1983 01:15:05:002");document.write(d.getUTCMilliseconds());

I've seen in Date object referance that they avoided the millisecond part in example this way-

var d = new Date("July 21, 1983 01:15:00");document.write(d.getUTCMilliseconds());

and so it displays 0 as result..

Link to post
Share on other sites

I don't think you can just append milliseconds to the time in that way. You probably have to set the milliseconds on the date object.

Link to post
Share on other sites
I don't think you can just append milliseconds to the time in that way......
No dear i do not wanna append the millisecond part....! rather i wanna append other parts(like: seconds,minutes,hours,year,month,day etc.) of Date object and return only millisecond part....According to the tutorial "The getUTCMilliseconds() method returns the milliseconds of the specified date and time..."But it returns "NaN" as result when i take a date object this way :
 var d=new Date("July 21, 1983 01:25:43:520");document.write(d.getUTCMilliseconds());

By the way, the example of getUTCMilliseconds() , provided in W3SCHOOLS tutorial is really very poor, for which i can't make out this method properly..

Link to post
Share on other sites

That's because the input string for the date object is not allowed to contain those three millisecond digits, which is what I was trying to say in my last post.You have to set the values like this:

var d = new Date();d.setYear( ... );d.setMonth( ... ); . . .d.setSeconds( ... );d.setMilliseconds( ... );document.write(d.getUTCMilliseconds());

Link to post
Share on other sites

This has nothing to do with getUTCMilliseconds() . It is all about the object constructor. This works:var d=new Date("July 21, 1983 01:25:43");This does not work:var d=new Date("July 21, 1983 01:25:43:520");The Date object constructor does not understand the ":520" at the end.----Obviously, you have a problem that needs a solution. Please speak more generally about the problem, and you might get a good answer. Making assumptions about the available solutions can lead to confusion, which you can see has already happened.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...