Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rearrange date format jQuery or javascript

Hello I have a function that generates the date with this format:

MM-DD-YYYY

Is there any jquery or javascript trick to convert that value into:

YYYY-MM-DD?

More Detailed Explanation:

The function I have generates the date and stored in a variable called tdate

So var tdate = 01-30-2001

I would like to do some jquery or javascript to turn tdate into:

tdate = 2001-01-30

tdate is a string

Thanks!

like image 936
cup_of Avatar asked Nov 22 '25 09:11

cup_of


1 Answers

You can use .split(), destructuring assignment, termplate literal to place yyyy, mm, dd in any order

var date = "01-30-2001";

var [mm, dd, yyyy] = date.split("-");

var revdate = `${yyyy}-${mm}-${dd}`;

console.log(revdate)
like image 181
guest271314 Avatar answered Nov 23 '25 21:11

guest271314



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!