0

Ich habe eine .cshtml mit einem daterangepicker.js implementiert. Diese Seite ruft den Datumsbereich (bis Datum, von Datum) von einer übergeordneten Seite über ViewBag ab. Der Code ist der folgende:Parameterpicker Moment() gibt Zahlen zurück

var _FromDate; 
var _EndDate; 

$(function() { 
    var from1 = '@ViewBag.from_date'; 
    var from = from1.split("-"); 
    var f = new Date(from[0], from[1] - 1, from[2]); 

    var to1 = '@ViewBag.to_date'; 
    var to = to1.split("-"); 
    var t = new Date(to[0], to[1] - 1, to[2]); 

    var start = (f.getFullYear() + '-' + pad((f.getMonth() + 1), 2) + '-' + pad(f.getDate(), 2)); 
    var end = (t.getFullYear() + '-' + pad((t.getMonth() + 1), 2) + '-' + pad(t.getDate(), 2)); 

    function cb(start, end) { 

     $('#reportrange span').html(start + ' . ' + end); 
     var dateRange = $('#reportrange span').html(); 
     _FromDate = dateRange.substring(0, dateRange.indexOf('.')); 
     _EndDate = dateRange.substring(dateRange.indexOf('.') + 1); 
    }; 

    function pad(str, max) { 
     str = str.toString(); 
     return str.length < max ? pad("0" + str, max) : str; 
    } 

    $('#reportrange').daterangepicker({ 
     startDate: start, 
     endDate: end, 
     locale: { format: "YYYY-MM-DD" }, 
     ranges: { 
      'Today': [moment(), moment()], 
      'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')], 
      'Last 7 Days': [moment().subtract(6, 'days'), moment()], 
      'Last 30 Days': [moment().subtract(29, 'days'), moment()], 
      'This Month': [moment().startOf('month'), moment().endOf('month')], 
      'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')] 
     } 
    }, cb); 

    cb(start, end); 

}); 

Jetzt ist hier das Problem: wenn die Seite ausgeführt wird, die daterangepicker das Datum als zurück a string of numbers

Was ist die Ursache des Problems? Ich bin Schritt für Schritt durch den Code gegangen und kann das Problem nicht finden.

Jede Hilfe wird geschätzt!

+0

können Sie die Werte von ** von1 ** und ** to1 hier es console.log zupfen hat ** und fügen Sie ihn, genau richtig nach ihrem jeweilige Erklärung ... –

+0

@ThirueswaranRajagopalan hier gehen Sie: from1 zeigt 2017-09-10 und to1 zeigt 2017-09-10 (weil auf der vorherigen Seite wählte ich 'Heute' so zeigt es die gleichen Werte für beide from1 und to1 – ragg

+0

Es ist alles gut. Seine nur '$ ('# reportrange span') .html (Start + '.' + Ende);' die ** Start ** und ** Ende ** sind konvertiert eine Unix-Zeitstempel (ms). –

Antwort

0

Sie haben einige unnötige Code, ich rewite https://embed.plnkr.co/G3xxp0HWPOoFYuF8u2qd/

/* global variables to store your dates ..*/ 
var _FromDate; 
var _EndDate; 

$(function() { 
    var _format = "YYYY-MM-DD"; 
    var start = moment('@ViewBag.from_date', _format); 
    var end = moment('@ViewBag.from_date', _format); 

    /* update your display summary and your local variables... */ 
    function cb(start, end) { 
    /* 
     storing the moment object. If case you post these values over to a server, defaulr ajax transforms into ISOstring. 
     But in case it doesn't, your should _FromDate = start.toISOString() or if u have a sppecific format start.format('{your format}') 
    */ 
    _FromDate = start; 
    _EndDate = end; 
    $('#reportrange span').html(start.format(_format) + ' - ' + end.format(_format)); 
    } 

    $('#reportrange').daterangepicker({ 
    startDate: start, 
    endDate: end, 
    locale: { format: _format }, 
    ranges: { 
     'Today': [moment(), moment()], 
     'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')], 
     'Last 7 Days': [moment().subtract(6, 'days'), moment()], 
     'Last 30 Days': [moment().subtract(29, 'days'), moment()], 
     'This Month': [moment().startOf('month'), moment().endOf('month')], 
     'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')] 
    } 
    }, cb); 

    cb(start, end); 

}); 
+0

Ich habe den Code hinzugefügt, aber jetzt erscheint ein Fehler in meinem Codebehind: Fehler beim Konvertieren von Datentyp Nvarchar nach Datum. Ich sende die Variablen _FromDate und _EndDate an den Codebehind, um Daten von einer gespeicherten Prozedur abzurufen. – ragg

+0

'_FromDate = start.format (_format)' BTW, habe es im Kommentar erwähnt ... –

+1

Es hat funktioniert! Vielen herzlichen Dank! – ragg

Verwandte Themen