Parsing 将日期/时间字段拆分为单独的日期和时间

Parsing 将日期/时间字段拆分为单独的日期和时间,parsing,datetime,split,datepicker,getdate,Parsing,Datetime,Split,Datepicker,Getdate,我有一个日期/时间字段(即2018-04-24 10:00:00),我想将其拆分为单独的日期和时间。我有以下函数,但它不适用于uib datepicker,因为我像字符串一样拆分日期/时间字段: function returnDate(date) { var apptDate = date.split(' ')[0]; return apptDate; } function returnTime(date) { var apptTime = date.split(' '

我有一个日期/时间字段(即2018-04-24 10:00:00),我想将其拆分为单独的日期和时间。我有以下函数,但它不适用于uib datepicker,因为我像字符串一样拆分日期/时间字段:

function returnDate(date) {
    var apptDate = date.split(' ')[0];
    return apptDate;
}

function returnTime(date) {
    var apptTime = date.split(' ')[1].substring(0,5);
    var hours24 = parseInt(apptTime.substring(0, 2),10);
    var hours = ((hours24 + 11) % 12) + 1;
    var amPm = hours24 > 11 ? 'pm' : 'am';
    var minutes = apptTime.substring(2);
    return hours + minutes + ' ' + amPm;
}
我也尝试过使用getDate、getFullYear、getMonth等,但getDate总是出现类型错误


有人能就这个日期问题提供一些指导吗?谢谢

您是否尝试过
新日期('2018-04-24 10:00:00')
,然后从日期对象获取月-年等后缀?

因为日期和时间之间有一个空格,所以您可以通过这种方式分别获取日期和时间

方法1:拆分字符串

string date_time = "2018-04-24 10:00:00";

string[] words = date_time.Split(' ');//Split string
string date = words[0];//date = 1st object (before space)
string time = words[1];//time= 2nd object (after space)
方法2:使用正则表达式

string date_time = "2018-04-24 10:00:00";               
string _date = "";    
string _time = "";

Regex date = new Regex(@"([0-9-]+)\s");            
Match match_date = date.Match(date_time);

Regex time = new Regex(@"\s([0-9:]+)");
Match match_time = time.Match(date_time);

//Date
if (match_date.Success)
{
    _date =  match_date.Value;
    Console.WriteLine(_date);
}
//Time
if (match_time.Success)
{
    _time = match_time.Value.Replace(" ","");
    Console.WriteLine(_time);
}