以毫秒为单位检索日期对象的日期部分

时间:2015-06-17 17:12:05

标签: javascript date datetime angular-ui-bootstrap milliseconds

如何在javascript中以毫秒为单位获取Date对象的日期部分?

我没有找到any method。我在努力:

var today = new Date();
var datePortion = today.toDateString();
var inMiliseconds = new Date(datePortion).getMilliseconds();
console.log(inMiliseconds);

返回始终为0。请参阅jsFiddle

我该怎么办?

化背景

我想要的是在两个单独的字段中获取日期和时间部分,然后添加两个值(以毫秒为单位)以获取新日期。 我在下面的HTML中使用angular-ui的datepicker和timepicker获取了这两个日期

<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>

我正在恢复控制器中的两个值,但它们是作为Date个对象发送的,所以我需要获取日期字段的日期部分和时间字段的时间部分(以毫秒为单位)

欢迎任何提示或建议。

1 个答案:

答案 0 :(得分:2)

0返回日期的毫秒部分,getTime()就是你的情况,因为你是从日期的某个日期部分(仅)构建的。

如果我正确理解了这个要求,那么你试图将日期表示作为epoc的毫秒数。这是通过var today = new Date(); var datePortion = today.toDateString(); var inMiliseconds = new Date(datePortion).getTime(); console.log(inMiliseconds); 方法完成的(我承认它对于该方法来说是一个不太精彩的名称)。所以,长话短说:

#define PasteBoardTypeHTML          @"public.html"

- (void) copyAsRTF
{
    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];

    // Get our RTF data
    NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
                                     documentAttributes: nil];

    // Get the HTML for our current data
    NSData *htmlData = [[self htmlForRange: self.selectedRange] dataUsingEncoding: NSUTF8StringEncoding];

    NSMutableArray * clipboardTypes = [NSMutableArray array];
    if (nil != rtfData)
    {
        [clipboardTypes addObject: NSPasteboardTypeRTF];
    } // End of we have rtf

    if(nil != htmlData)
    {
        [clipboardTypes addObject: PasteBoardTypeHTML];
    } // End of we have html

    // Set our pasteboard types (types need to be declared before we call setData).
    [pasteboard declareTypes: clipboardTypes.copy
                       owner: self];

    if (rtfData)
    {
        [pasteboard setData: rtfData
                   forType: NSPasteboardTypeRTF];
    } // End of we have rtf

    if(htmlData)
    {
        [pasteboard setData: htmlData
                   forType: PasteBoardTypeHTML];
    } // End of we have html
} // End of copyAsRTF

- (NSString *) htmlForRange: (NSRange) range
{
    NSMutableString * htmlOutput = [NSMutableString stringWithString: @"<meta charset='utf-8'><pre>"];

    [[self textStorage] enumerateAttributesInRange: range
                                           options: 0
                                        usingBlock:
     ^(NSDictionary * attributes, NSRange range, BOOL * stop)
     {
         NSString * actualCode = [[self textStorage].string substringWithRange: range];
         actualCode = [self textToHtml: actualCode];

         NSMutableString * currentHtml = [NSMutableString stringWithString: @"<span"];
         NSColor * color = attributes[NSForegroundColorAttributeName];
         NSFont  * font  = attributes[NSFontAttributeName];

         NSMutableArray * fontStyles = [NSMutableArray array];

         if(nil != color)
         {
             NSString * fontColorStyle =
                [NSString stringWithFormat: @"color: %@;", [color hexadecimalValue]];

             [fontStyles addObject: fontColorStyle];
         } // End of we have a color

         if(nil != font)
         {
             NSString * fontDetailsStyle =
             [NSString stringWithFormat: @"font-family: %@;", font.familyName];
             [fontStyles addObject: fontDetailsStyle];
         } // End of we have a font

         if(nil != fontStyles && fontStyles.count > 0)
         {
             [currentHtml appendFormat: @" style=\"%@\"", [fontStyles componentsJoinedByString: @" "]];
         } // End of we have font styles

         [currentHtml appendString: @">"];

         [currentHtml appendString: actualCode];
         [currentHtml appendString: @"</span>"];

         // Add our section
         [htmlOutput appendString: currentHtml];
     }]; // End of attribute enumerations

    // End of html output
    [htmlOutput appendString: @"</pre></meta>"];

    return htmlOutput.copy;
} // End of htmlForRange:

- (NSString*)textToHtml:(NSString*)htmlString
{
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&"  withString:@"&amp;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<"  withString:@"&lt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@">"  withString:@"&gt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"""" withString:@"&quot;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'"  withString:@"&#039;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    return htmlString;
} // End of textToHtml: