0

i'm new to HTML and JavaScript but i have to change a string which represents a date in this format: DD/MM/YYYY HH:MM:SS to this format: DD/MM/YYYY HH:MM. I am trying to get the value of {{msr.start}} inside of my javascript function so that i can use the slice() function to make it 3 characters shorter, can anyone please tell me how i can get the value of {{msr.start}} inside of my script? Thanks in advance :)

<td colspan="4" class="text-center text-middle" id="abc123">
  {{msr.start}} <--- this contains the right date and time
  <script>
    var tmp = {{msr.start}}.splice(0, 3) <--- here is where i want to use the date and time
    document.getElementById('abc123').innerHTML = tmp
  </script>
4
  • 2
    If {{msr.start}} outputs the plain date string, then you will have to put quotes around this, to make it a text literal in JavaScript. Commented Jul 22, 2021 at 12:42
  • basicaly i can use the {{msr.start}}'s value in my html code but when i open the <script> i want to be able to access the value of {{msr.start}} Commented Jul 22, 2021 at 12:45
  • No need to repeat yourself, we already know that. Need to pay attention though, because I already told you what you need to do. (Assuming this is a server-side templating language you are using there, that information is currently missing from your explanation.) Commented Jul 22, 2021 at 12:50
  • I'm sorry I was having a hard time understanding but now that i saw how to do it I realized that you were right! Thank you very much for your time and effort I realy appreciate it :-) Commented Jul 22, 2021 at 13:01

1 Answer 1

2

You can use let tmp = "{{msr.start}}" and then use substring() Example:

let tmp = "DD/MM/YYYY HH:MM:SS" //You would use "{{msr.start}}"

console.log(tmp.substring(0, tmp.length - 3));

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

6 Comments

the problem is that {{msr.start}} is in my html code and i don't know how to get it inside of the <script>
What is the actual content of {{msr.start}} is it an entire html element?
no it's not a html element, it's the variable that contains the time and date, my code used to be like this: <td colspan="4" class="text-center text-middle">{{msr.start}}</td> and it would print the time and date but now i have to find a way to remove the seconds from {{msr.start}}
“the problem is that {{msr.start}} is in my html code and i don't know how to get it inside of the <script>” - your script element is also HTML, so it works the exact same way. All you need to do, is make the result into actually valid JS syntax.
@matthewfabrie, we've all been there.
|

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.