Convert date to unix timestamp JavaScript – How to convert date to timestamp in JavaScript?

Convert date to unix timestamp JavaScript Date: Create, Convert, Compare Dates. Convert the unix timestamp into milliseconds by multiplying it by 1000.

Convert date to unix timestamp JavaScript

const join_at = new Date("Jan 10, 2022");

const timestamp = join_at.getTime();

Date() Syntax

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)
new Date(year, monthIndex, day, hours)
new Date(year, monthIndex, day, hours, minutes)
new Date(year, monthIndex, day, hours, minutes, seconds)
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

uses the Date.parse() method

function toTimestamp(strDate){
   var datum = Date.parse(strDate);
   return datum/1000;
alert(toTimestamp('02/13/2022 23:31:30'));

Example: Date In JavaScript



var currentDate = new Date();

Example: Create Date by Specifying Milliseconds

var join_at1 = new Date(0);  // Thu Jan 01 1970 05:30:00

var join_at2 = new Date(1000); // Thu Jan 01 1970 05:30:01

var join_at3 = new Date(5000); // Thu Jan 01 1970 05:30:05 

Example: Create Date by Specifying Date String

var join_at1 = new Date("3 march 2022");

var join_at2 = new Date("3 February, 2022");

var join_at3 = new Date("3rd February, 2022"); // invalid date

var join_at4 = new Date("2022 3 February");

var join_at5 = new Date("3 2022 February ");

var join_at6 = new Date("February 3 2022");

var join_at7 = new Date("February 2022 3");

var join_at8 = new Date("2 3 2022");

var join_at9 = new Date("3 march 2022 20:21:44");

Example: Create Date using Different Date Separator

var join_at1 = new Date("February 2022-3");

var join_at2 = new Date("February-2022-3");

var join_at3 = new Date("February-2022-3");

var join_at4 = new Date("February,2022-3");

var join_at5 = new Date("February,2022,3");

var join_at6 = new Date("February*2022,3");

var join_at7 = new Date("February$2022$3");

var join_at8 = new Date("3-2-2022"); // MM-dd-YYYY

var join_at9 = new Date("3/2/2022"); // MM-dd-YYYY

don’t Miss : Convert Date To Timestamp Javascript

Convert Date Formats

var join_at = new Date('2022-02-10T10:12:50.5000z');

join_at; 'Default format:'

join_at.toDateString();'Tue Feb 10 2022'


join_at.toGMTString(); 'GMT format' 

join_at.toISOString(); '2022-02-10T10:12:50.500Z' 

join_at.toLocaleString();'Local date Format '

join_at.toLocaleTimeString(); 'Locale time format '

join_at.toString('YYYY-MM-dd'); 'Tue Feb 10 2022 15:42:50'

join_at.toTimeString(); '15:42:50' 

join_at.toUTCString(); 'UTC format ' 

Compare Dates in JavaScript

var join_at1 = new Date('4-1-2022');
var join_at2 = new Date('4-2-2022');

if (join_at1 > join_at2)
    alert(join_at1 + ' is greater than ' + join_at2);
else (join_at1 < join_at2 )
    alert(join_at1 + ' is less than ' + join_at2);

I hope you get an idea about Convert date to unix timestamp JavaScript.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.