Remove time from GMT time format

前端 未结 8 2537
有刺的猬
有刺的猬 2021-02-18 23:01

I am getting a date that comes in GMT format, Fri, 18 Oct 2013 11:38:23 GMT. The problem is that the time is messing up the timeline that I am using.

How can I strip ou

相关标签:
8条回答
  • 2021-02-18 23:29

    In this case you can just manipulate your string without the use of a Date object.

    var dateTime = 'Fri, 18 Oct 2013 11:38:23 GMT',
        date = dateTime.split(' ', 4).join(' ');
        
    document.body.appendChild(document.createTextNode(date));

    0 讨论(0)
  • 2021-02-18 23:31

    Just cut it with substring:

     var str = 'Fri, 18 Oct 2013 11:38:23 GMT';
     str = str.substring(0,tomorrow.toLocaleString().indexOf(':')-3);
    
    0 讨论(0)
  • 2021-02-18 23:34

    Like this:

    var dateString = 'Mon Jan 12 00:00:00 GMT 2015';
    dateString = new Date(dateString).toUTCString();
    dateString = dateString.split(' ').slice(0, 4).join(' ');
    console.log(dateString);
    
    0 讨论(0)
  • 2021-02-18 23:38

    If you want to keep using Date and not String you could do this:

    var d=new Date(); //your date object
    console.log(new Date(d.setHours(0,0,0,0)));
    

    -PS, you don't need a new Date object, it's just an example in case you want to log it to the console.

    http://www.w3schools.com/jsref/jsref_sethours.asp

    0 讨论(0)
  • 2021-02-18 23:40

    Well,

    Here is my Solution

    let dateString = 'Mon May 25 01:07:00 GMT 2020';
    let dateObj = new Date(dateString);
    
    console.log(dateObj.toDateString());
    // outputs Mon May 25 2020

    See its documentation on MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toDateString

    0 讨论(0)
  • 2021-02-18 23:41

    You can first convert the date to String:

    String dateString = String.valueOf(date);

    Then apply substring to the String:

    dateString.substring(4, 11) + dateString.substring(30);

    You need to take care as converting date to String will actually change the date format as well.

    0 讨论(0)
提交回复
热议问题