0

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)

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks the .toLocaleString is a possible option. I think I figured out how to replace the GMT text however.
0

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)," ")

Comments

0

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>");

Comments

0
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>");

2 Comments

This results in a date format like so without time: Thu,Jan,02,2020
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.