Javascript set time string to date object

后端 未结 5 949
野性不改
野性不改 2020-12-06 05:13

Please see the below code;

var d = new Date();
var s = \"01.00 AM\";
d.setTime(s);

I know this code is wrong. Please give me the correct wa

相关标签:
5条回答
  • 2020-12-06 05:28

    I'm late to the party, but I thought I'd share a funtion that doesn't use regex:

    function setDateTime(date, time) {
        var index = time.indexOf("."); // replace with ":" for differently displayed time.
        var index2 = time.indexOf(" ");
    
        var hours = time.substring(0, index);
        var minutes = time.substring(index + 1, index2);
    
        var mer = time.substring(index2 + 1, time.length);
        if (mer == "PM"){
            hours = hours + 12;
        }
    
    
        date.setHours(hours);
        date.setMinutes(minutes);
        date.setSeconds("00");
    
        return date;
    }
    
    0 讨论(0)
  • 2020-12-06 05:49

    I added a few things as an improvement to the accepted answer by @thebreiflabb to suit my use case and thought i'd share.

    if the regex is changed to timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;, it'll handle a few other common cases.

    namely:

    • using a colon instead of decimal point between hours and minutes
    • allowing am/pm to immediately follow the time with no space

    also, setting the seconds to 0 (since that was my main use case)

    the resulting code would be:

    var test, parts, hours, minutes, date,
        d = (new Date()).getTime(),
        tests = ['01.25 PM', '01:25pm', '1:25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
        i = tests.length,
        timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;
    
    for(; i-- > 0;) {
        test = tests[i];
    
        parts = test.match(timeReg);
    
        hours = /am/i.test(parts[3]) ?
            function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) :
            function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10));
    
        minutes = parseInt(parts[2], 10);
    
        date = new Date(d);
    
        date.setHours(hours);
        date.setMinutes(minutes);
        date.setSeconds(0);
    
        console.log(test + ' => ' + date);
    }
    
    0 讨论(0)
  • 2020-12-06 05:51

    You can parse the time with a regex, and set the hours and minutes accordingly:

    http://jsfiddle.net/54VkC/1/

    var d = new Date(),
        s = "01.25 PM",
        parts = s.match(/(\d+)\.(\d+) (\w+)/),
        hours = /am/i.test(parts[3]) ? parseInt(parts[1], 10) : parseInt(parts[1], 10) + 12,
        minutes = parseInt(parts[2], 10);
    
    d.setHours(hours);
    d.setMinutes(minutes);
    
    alert(d);
    

    Edit 1: As jaisonDavis pointed out, the original code will not work for AM or PM for 12.XX, which was an oversight since I never use 12-hour format myself, thinking it started at 00.00 which was wrong.

    The corrected code which handles these cases can be seen here:

    http://jsfiddle.net/54VkC/93/

    var test, parts, hours, minutes, date,
        d = (new Date()).getTime(),
        tests = ['01.25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'],
        i = tests.length,
        timeReg = /(\d+)\.(\d+) (\w+)/;
    
    for(; i-- > 0;) {
        test = tests[i];
        
        parts = test.match(timeReg);
        
        hours = /am/i.test(parts[3]) ?
            function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) :
            function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10));
        
        minutes = parseInt(parts[2], 10);
        
        date = new Date(d);
        
        date.setHours(hours);
        date.setMinutes(minutes);
        
        console.log(test + ' => ' + date);
    }

    0 讨论(0)
  • 2020-12-06 05:52

    Using moment.js will be the best solution.

    const fullDate = new Date();
    // fullDate = Tue Dec 12 2017 11:18:30 GMT+0530 (IST) {}
    const time = '01.00 AM';
    const d = moment(fullDate).format('L'); // d = "12/12/2017"
    const date = moment(d +' '+ time).format();
    // date = "2017-12-12T01:00:00+05:30"
    

    If you want convert moment date to js date

    const jsDate = moment(date).toDate();
    
    0 讨论(0)
  • 2020-12-06 05:52
    function getCurrentDate() {
        var lDate = new Date();
        var lDay = lDate.getDate();
        var lMonth = lDate.getMonth() + 1;
        var lYear = lDate.getFullYear();
    
        if (lDay < 10) {
            lDay = '0' + lDay
        }
    
        if (lMonth < 10) {
            lMonth = '0' + lMonth
        }
        mCurrentDate = lYear + "-" + lMonth + "-" + lDay + "T00:00:00+05:30";
    }
    
    0 讨论(0)
提交回复
热议问题