PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 2, 2022

[FIXED] How to add Current Date and Time into Table?

 August 02, 2022     datatable, datetime, function, html-table, javascript     No comments   

Issue

how can I add the current date and time with this format: 2022-07-17 17:50:20?

I already managed it with an input value but I need the current time.


Solution

You should use built-in Date object in Javascript, like this:

let currentdate = new Date(); 
let datetime = currentdate.getFullYear() + "-"
                + String((currentdate.getMonth() + 1)).padStart(2,"0")  + "-" 
                + String(currentdate.getDate()).padStart(2,"0") + " "
                + String(currentdate.getHours()).padStart(2,"0") + ":"  
                + String(currentdate.getMinutes()).padStart(2,"0") + ":" 
                + String(currentdate.getSeconds()).padStart(2,"0");
//The `+1` on the getMonth() is used because in Javascript January is 0.
console.log(datetime);

By the way, I suggest you make all your vars into lets as that's the current standard. There's a very fine difference between them (let is block scoped while var is functional scoped).



Answered By - TheBooker66 aka Ethan
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing