标签云

微信群

扫码加入我们

WeChat QR Code

How do I get current date in JavaScript?


var currentTime = new Date();

2019年06月25日20分57秒

See the documentation for the Date object. It has examples.

2019年06月25日20分57秒

this would help you tizag.com/javascriptT/javascriptdate.php

2019年06月26日20分57秒

new Date() returns the current time, not the current date. The distinction matters if you're trying to compare it against another date which doesn't have a time component (ie, is at midnight).

2019年06月25日20分57秒

use momentJs, this lib is gold for developers.

2019年06月25日20分57秒

thanks for the code.. but what I still don't get it, is the line if(dd<10){dd='0'+dd} ... why < 10? from what I understand from the code is if day's character is less than 2, just add a preceding 0 in front of the day.. but why 10?

2019年06月26日20分57秒

imin: because less than 2 characters means 1 character... and everything under 10 (1 to 9) is 1 character, so we'll have 01, 02, ..., 09

2019年06月26日20分57秒

MounaCheikhna - How could we be in the year 999?

2019年06月26日20分57秒

Swap around the month and date if you're not in north America.

2019年06月26日20分57秒

The new Date.prototype.toLocaleDateString() method is a more flexible solution. It's a part of JavaScript since ECMAScript 5.1 and is well-supportedby evergreen browsers. MDN: toLocaleDateString()

2019年06月25日20分57秒

Looks to good to be true. Any downsides to this?

2019年06月25日20分57秒

I dont think so :) Seems pretty straightforward!

2019年06月26日20分57秒

toJSON() returns as utc datetime

2019年06月26日20分57秒

It doesn't consider TimezoneOffset.At my time of testing, I was seeking "now" and I got "yesterday". stackoverflow.com/questions/13646446/…

2019年06月25日20分57秒

Perfect. This is the cleanest way to do this I'm seeing here. Works well in MomentJS for "Today, Not Now" moment( new Date().toJSON().slice(0, 10) )

2019年06月25日20分57秒

KamranAhmed Almost 2 years and 40+ votes later, I'd say the effort was worth it. LoL. I've since expanded this class alot personally, but haven't uploaded as I was thinking most people would use that other js date plugin i see recommended, but I guess I should make it more "public" and add it up here.

2019年06月26日20分57秒

moment.js is now the thing you would use these days

2019年06月25日20分57秒

Thumbs up for the effort and light weight!

2019年06月25日20分57秒

Above, there is a typo (that took me awhile to spot), it uses a variable "today" in the line: " dayOfMonth = today + "

2019年06月26日20分57秒

"today + ( objToday.getDate() < 10) ? '0' + objToday.getDate() + domEnder : objToday.getDate() + domEnder" - JS is stupid language.

2019年06月26日20分57秒

This still fails due to time zone shift.

2019年06月26日20分57秒

Why is this not the accepted answer, simplest and uses built in functions

2019年06月25日20分57秒

This seems to be the only answer that actually answers the question. Everyone else answers on how to format a date as string.

2019年06月26日20分57秒

++ best answer in this thread

2019年06月25日20分57秒

Indeed. This is the correct answer to the problem.

2019年06月25日20分57秒

I agree. Actually I wanted to write an answer similar to this one and then a small window popped up asking me whether I've read through all the answers. Since I've read only the top answers, I decided to check whether there are any correct answers and this one was the first correct answer.

2019年06月25日20分57秒

Never use document.write() DOM manipulation, consider either console.log() or other options

2019年06月25日20分57秒

This is the best time lib I have found!

2019年06月26日20分57秒

Supports localization like a charm.

2019年06月26日20分57秒

These days we use date-fns - it treats dates as Immutable (Moment mutates dates), is faster and is modular (just import what you need).

2019年06月26日20分57秒

Yeah, I couldn't agree more.

2019年06月25日20分57秒

Six years after posting this momentjs has put on quite a bit of weight. You may want to check out github.com/iamkun/dayjs instead - I've had it described to me as "momentjs on a diet". Same simple API.

2019年06月25日20分57秒

Best answer, MomentJs looks awesome.

2019年06月26日20分57秒

Moment is overkill for just getting the current date.

2019年06月25日20分57秒

Or moment().format("L") to respect the current locale.

2019年06月25日20分57秒

DanDascalescu Actually, the Javascript base specification for DateTime is that bad.

2019年06月25日20分57秒

If you already have momentjs imported in your project then this is the cleanest simplest answer.

2019年06月26日20分57秒

I needed a time in 00:00:00 and didn't want to rebuild it manually; step 2 gets me there perfectly. Kudos!

2019年06月25日20分57秒

You can save some bytes by doing this: Date().split(' ').splice(1,3).join(' ')

2019年06月25日20分57秒

Thanks for the break down!

2019年06月26日20分57秒

Never use document.write() DOM manipulation, consider either console.log() or other options

2019年06月25日20分57秒

Never use document.write() DOM manipulation, consider either console.log() or other options

2019年06月25日20分57秒

Updated, thanks jasonleonhard :)

2019年06月25日20分57秒

Glad that helped.

2019年06月25日20分57秒

Nice, works on Chrome. Unfortunately doesn't work on PhantomJS as of 22/4/2016

2019年06月25日20分57秒

cool solution. should be on the top. new Date().toLocaleDateString("de-de") does the trick for me.

2019年06月25日20分57秒

Yay, finally...

2019年06月25日20分57秒

if you are using jquery ui with a datepicker, you can use $.datepicker.formatDate('yy/mm/dd', new Date())

2019年06月26日20分57秒

This was basically all I needed.var dtToday = new Date(date.now);

2019年06月26日20分57秒

Best answer...Much better than the marked answer. Thank you.

2019年06月26日20分57秒

I was looking for a very simple seed function that changes regularly. This answer is a real treat.

2019年06月25日20分57秒

It would return mistakes, like this 3/4/2018, , better to use new Date().toJSON().slice(0,10).

2019年06月26日20分57秒

This is perfect for simply getting a date for view or for info on console log or for UI.Better for me without the .slice(0,10)

2019年06月26日20分57秒

This will return data+time, eg. '2018-4-6 16:20:22', and the question is how to get date only.

2019年06月26日20分57秒

Do we really need another answer like that?

2019年06月26日20分57秒

I did not find this method in any of the answers so added it

2019年06月25日20分57秒