0

Sorry, but what is the fastest way to display the current date?

 2014-01-18 Saturday 12:30

with this function or how do it the right way?

 var d=new Date();
 var t=d.getTime();
2

4 Answers 4

2

Try

var d = new Date();
var dd = d.getDate();
var mm = d.getMonth()+1; //January is 0!
var yy = d.getFullYear();

var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

var day=weekday[d.getDay()];

var h = d.getHours();
    var m = d.getMinutes();

alert(yy+"-"+mm+"-"+dd+" "+day+" "+h+":"+m)

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1
var d =  new Date();
alert(d.toString());

2 Comments

Just a note: there's no need to explicitly use .toString()
true but here I have just given .toString() for to be in safe side. I assumed there would be further operations on the variable like storing in some other variable or smthing.... So depends on the use case
1

If you don't mind the format, you can do it in one line:

''+new Date()

You only need to use a Date object as a string, in order to implicitly call its .toString() method, which

returns a String value. The contents of the String are implementation-dependent, but are intended to represent the Date in the current time zone in a convenient, human-readable form.

Comments

1
new Date().toGMTString()

It's something similar to what you are looking for

If you want complicate the output you can get element by element and format yourself the date (or you can use Globalize.js)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.