Andrew
Andrew

Reputation: 395

Remove the GMT-#### text from a date in javascript?

I would like to remove the "GMT-####" text from the results. Date/time results will differ based on the search criteria the user enters.

    for (var j = 0; j < resultCountGLM; j++) {

            var featureAttributes2 = results[1].features[j].attributes;
            //console.log(featureAttributes2)

            if (attr = "DATE_UTC") {
                attr1 = "Detected during the 1-hour period beginning"
                var dateGM = new Date(featureAttributes2[attr]);
                resultItems.push("<p class='reportTextResults_font'><b>" + attr1 + ":</b>  " + dateGM + "</p>");
            }
        }
        resultItems.push("<br>");

Example Output:

    Detected during the 1-hour period beginning: Thu Jan 02 2020 14:00:00 GMT-0600 (Central Standard Time)
    Detected during the 1-hour period beginning: Thu Jan 02 2020 16:00:00 GMT-0600 (Central Standard Time)

Desired Output:

    Detected during the 1-hour period beginning: Thu Jan 02 2020 14:00:00 (Central Standard Time)
    Detected during the 1-hour period beginning: Thu Jan 02 2020 16:00:00 (Central Standard Time)

Upvotes: 0

Views: 594

Answers (4)

Aldrin
Aldrin

Reputation: 2194

let dateGM = new Date(featureAttributes2[attr]).toString().split(" ");
dateGM.splice(5,1) //remove GMT-####
dateGM.join(" ");
resultItems.push("<p class='reportTextResults_font'><b>" + attr1 + ":</b>  " + dateGM + "</p>");

Upvotes: 0

I_Al-thamary
I_Al-thamary

Reputation: 4003

You can use it this way if you need only the date and I give a different option because local time may affect the results.

<!DOCTYPE html>
<html>
<body>


 <p id="demo4"></p>
<p id="demo5"></p>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
   
<script>
var dateGM = new Date('Thu Jan 02 2020 14:00:00 GMT-0600');
var pre='GMT'

var date = dateGM.toString().slice(0, dateGM.toString().lastIndexOf(pre));

var myDate = new Date(date);
    var    dateString=dateGM.toISOString().split('t')[0];
 var dateString1=dateGM.toISOString();

 var dateString2=dateGM.toISOString().split('T')[0];
 var dateString3=dateGM.toISOString().split('.')[0]    
 var dateStringnew  =dateGM.toDateString('YYYY-MM-DD HH:mm:ss')+" "+dateGM.toLocaleTimeString('it-IT');   
//console.log(dateStringnew);
document.getElementById("demo").innerHTML = dateString;
document.getElementById("demo1").innerHTML = dateString1;
document.getElementById("demo2").innerHTML = dateString2;
document.getElementById("demo3").innerHTML = dateString3;
document.getElementById("demo4").innerHTML = date;
document.getElementById("demo5").innerHTML = dateStringnew;
</script>

</body>
</html>

Your code can be

 for (var j = 0; j < resultCountGLM; j++) {

            var featureAttributes2 = results[1].features[j].attributes;
            //console.log(featureAttributes2)

            if (attr = "DATE_UTC") {
                attr1 = "Detected during the 1-hour period beginning"
                var dateGM = new Date(featureAttributes2[attr]);
                 var pre='GMT'

             var date = 
        dateGM.toString().slice(0,dateGM.toString().lastIndexOf(pre));
                resultItems.push("<p class='reportTextResults_font'><b>" + attr1 + ":</b>  " + date + "</p>");
            }
        }
        resultItems.push("<br>");

Upvotes: 0

Andrew
Andrew

Reputation: 395

I went with the following below. The GMT text will always be 8 characters.

    var dateGM = new Date(featureAttributes2[attr]);
    var stringGM = dateGM.toString();
    var n = stringGM.indexOf("GMT")
    var dateGMfinal = stringGM.replace(stringGM.substring(n,n+8)," ")

Upvotes: 0

Mathew Berg
Mathew Berg

Reputation: 28750

If you want to use vanilla javascript you have two options. Either remove it yourself or look at one of the .toXXX functions. Example:

new Date().toLocaleString()
//"1/14/2020, 1:05:58 PM"

If you need it to be exactly that or potentially more granularity down the line, there's lots of libraries out there that help with formatting dates.

Upvotes: 1

Related Questions