How can I get the date portion of a Date
object in milliseconds in javascript?
I didn't find any method to this. I was trying:
var today = new Date();
var datePortion = today.toDateString();
var inMiliseconds = new Date(datePortion).getMilliseconds();
console.log(inMiliseconds);
The return is always 0
. See jsFiddle.
What can I do?
Backgroud:
What I want is get the date and time portions in two separated fields and then add the two values (in milliseconds) to get new date. I'm getting the two dates using the angular-ui's datepicker and timepicker in the HTML below
<div class="col-md-2">
<label for="InitialDate">Date:</label>
<div id="InitialDate" class="input-group">
<input type="text" name="Initial" data-datepicker-popup="yyyy/MM/dd" data-ng-model="date"
data-is-open="datepickers.date" data-datepicker-options="dateOptions" data-date-disabled="disabled(date, mode)" data-ng-required="true"/>
<span class="input-group-btn">
<button type="button" class="btn btn-default" data-ng-click="open($event)"><i class="fa fa-calendar"></i></button>
</span>
</div>
<timepicker data-ng-model="hour" data-ng-change="changed()" data-hour-step="1" data-minute-step="5" data-show-meridian="ismeridian"></timepicker>
</div>
I'm recovering the two values in my controller, but they are sent as Date
objects, so I need to get the date portion of the date field and the time portion of the time field in milliseconds.
Any tip or advice are welcome.
getMilliseconds()
returns the milliseconds part of the date, which is0
is your case, since you constructed it from the date part (only) of a date.If I understand the requirement correctly, you're trying to get the date's representation as the number of milliseconds from the epoc. This is done by the
getTime()
method (I concede it's a less-than-brilliant name for that method). So, long story short: