Parse DateTime string w JavaScript

Czy ktoś wie jak parsować łańcuch daty w wymaganym formacie dd.mm.yyyy?

Author: abatishchev, 2009-10-16

9 answers

Zobacz:

Kod:

var strDate = "03.09.1979";
var dateParts = strDate.split(".");

var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
 139
Author: Jonathan Fingland,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-01 14:13:45

Jeśli używasz jQuery UI, możesz sformatować dowolną datę za pomocą:

<html>
    <body>
        Your date formated: <span id="date1"></span><br/>
    </body>
</html>

 

var myDate = '30.11.2011';
var parsedDate = $.datepicker.parseDate('dd.mm.yy', myDate);

$('#date1').text($.datepicker.formatDate('M d, yy', parsedDate));

Http://jsfiddle.net/mescalito2345/ND2Qg/14/

 53
Author: Mecalito,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-10 05:11:02

Używamy tego kodu, aby sprawdzić, czy łańcuch jest poprawną datą

var dt = new Date(txtDate.value)
if (isNaN(dt))
 9
Author: Adriaan Stander,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-16 08:16:27

Refs: http://momentjs.com/docs/#/parsing/string/

Jeśli użyjesz chwili.js, możesz użyć trybu "string" + "format"
moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);

Ex:

moment("12-25-1995", "MM-DD-YYYY");
 6
Author: hiveer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-30 06:52:47

Użyj obiektu Date:

var time = Date.parse('02.02.1999');
document.writeln(time);

Daj: 917902800000

 4
Author: HotJard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-23 15:30:32

I ' V został użyty następujący kod w IE. (Zgodny z IE8)

var dString = "2013.2.4";
var myDate = new Date( dString.replace(/(\d+)\.(\d+)\.(\d+)/,"$2/$3/$1") );
alert( "my date:"+ myDate );
 3
Author: user3444722,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-10 02:52:30

ASP.NET programiści mają do wyboru Ten poręczny wbudowany (MS JS musi być zawarte na stronie):

var date = Date.parseLocale('20-Mar-2012', 'dd-MMM-yyyy');

Http://msdn.microsoft.com/en-us/library/bb397521%28v=vs.100%29.aspx

 2
Author: Matthew Millman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-10 08:53:30

Ta funkcja obsługuje również nieprawidłową datę 29.2.2001.

function parseDate(str) {
    var dateParts = str.split(".");
    if (dateParts.length != 3)
        return null;
    var year = dateParts[2];
    var month = dateParts[1];
    var day = dateParts[0];

    if (isNaN(day) || isNaN(month) || isNaN(year))
        return null;

    var result = new Date(year, (month - 1), day);
    if (result == null)
        return null;
    if (result.getDate() != day)
        return null;
    if (result.getMonth() != (month - 1))
        return null;
    if (result.getFullYear() != year)
        return null;

    return result;
}
 2
Author: Martin Staufcik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-31 10:09:49

Możesz sformatować datę właśnie wykonując tego typu code.In javascript.

 // for eg.
              var inputdate=document.getElementById("getdate").value);
                 var datecomp= inputdate.split('.');

                Var Date= new Date(datecomp[2], datecomp[1]-1, datecomp[0]); 
                 //new date( Year,Month,Date)
 0
Author: Bachas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-21 06:35:58