How can I get this timestamp format with javascript / jquery?

jquery timestamp format
javascript timestamp format
javascript date format
jquery get current date time format
javascript date format yyyy-mm-dd
javascript convert timestamp to date format
js timestamp
jquery date format

I want to get this format :

2019-03-24 15:05:20

Here is what I have tried:

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;

I got:

2019-3-24 15:0:20

It's missing leading zeroes.

That's because numbers lower than 10 should be padded with zero but they aren't.

You can use padStart function to achieve the expected result - add a leading 0 character if the length of a number is less than 2. I modified your code a little bit but all you need is to use .toString().padStart(2, '0') on a number.

var today = new Date();

var date = [
  today.getFullYear(),
  today.getMonth() + 1,
  today.getDate(),
].map((value) => value.toString().padStart(2, '0')).join('-');

var time = [
  today.getHours(),
  today.getMinutes(),
  today.getSeconds(),
].map((value) => value.toString().padStart(2, '0')).join(':');;


var dateTime = date + ' ' + time;
console.log(dateTime);

JavaScript Date, Date Formats. JavaScript supports ISO 8601 date format by default - YYYY-MM- DDTHH:mm:ss.sssZ. Example: ISO Date� Simple jQuery code snippet to get the time since an event happened. Useful for tracking how long the user is taking to interact with the page elements.

This is pretty close to ISO 8601, so let's start with that.

const d = new Date();
d
  .toISOString() // Convert date to a string in the format of 2019-03-25T00:07:22.0253Z
  .substr(0, 19)  // Strip off the milliseconds and Zulu timezone indication
  .replace('T', ' '); // Replace the T for "time" with a space

This leaves you with a date formatted like 2019-03-25 00:07:22.

jQuery event.timeStamp Property, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� Answer: Use the Date.now () Method You can simply use the JavaScript Date.now () method to generate the UTC timestamp in milliseconds (which is the number of milliseconds since midnight Jan 1, 1970). The following example demonstrates how to get a timestamp and how to convert it back to the human-readable date and time format in JavaScript.

You can use ternary operation for simple use. This should give you leading zero.

        var today = new Date();
        var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
        var time = today.getHours() + ":" + (today.getMinutes() < 10 ? '0' : '') + today.getMinutes() + ":" + today.getSeconds();
        var dateTime = date + ' ' + time;

        console.log(dateTime, 'result');

JavaScript getHours() Method, and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. Date, a native object in JavaScript is the way we get all data about time. Just be careful in JavaScript the timestamp depends on the client computer set, so it's not 100% accurate timestamp. To get the best result, you need to get the timestamp from the server-side. Anyway, my preferred way is using vanilla.

You can use:

var d = new Date();
d = new Date(d.getTime() - 3000000);
var date_format_str = d.getFullYear().toString()+"-"+((d.getMonth()+1).toString().length==2?(d.getMonth()+1).toString():"0"+(d.getMonth()+1).toString())+"-"+(d.getDate().toString().length==2?d.getDate().toString():"0"+d.getDate().toString())+" "+(d.getHours().toString().length==2?d.getHours().toString():"0"+d.getHours().toString())+":"+((parseInt(d.getMinutes()/5)*5).toString().length==2?(parseInt(d.getMinutes()/5)*5).toString():"0"+(parseInt(d.getMinutes()/5)*5).toString())+":00";
console.log(date_format_str);

JavaScript getMonth() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� JavaScript Get Timestamp: You can use Date.now () to get the UTC Timestamp in milliseconds. Date.now () Works for all major browsers. Here in this tutorial we are going to explain how to get current timestamp in javaScript. We will also learn how to use the timestamp in javaScript.

Convert Unix timestamp to Date time with JavaScript, Unix timestamp value conversion with JavaScript mainly requires when the API request response contains the date time value in Unix format� Using JavaScript native Date functions you can get hours, minutes and seconds as you want. If you wish to format date and time in particular way you may want to implement a method extending JavaScript Date prototype.

event.timeStamp, getting the event.timeStamp value at two points in the code and noting the difference. <script src="https://code.jquery.com/jquery-3.5.0.js"></script>. </ head>. JavaScript ISO Dates. ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:

How to convert Unix timestamp to time in JavaScript?, This string is the time representation of the UNIX timestamp. The final formatted date is created by converting each of these values to a string using the toString() method and then How to convert jQuery to JavaScript ? To get the unix timestamp using JavaScript you need to use the getTime() function of the build in Date object. As this returns the number of milliseconds then we must divide the number by 1000 and round it in order to get the timestamp in seconds. Math.round(newDate().getTime()/1000);

Comments
  • Why was this downvoted? It's a perfectly reasonable solution to the problem presented, and is well documented. I really don't understand Stack Overflow these days.