poppel
poppel

Reputation: 1593

JS - How to get and calculate the current date

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();

Upvotes: 0

Views: 91

Answers (4)

franco
franco

Reputation: 141

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)

Upvotes: 1

Oriol
Oriol

Reputation: 287980

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.

Upvotes: 1

laaposto
laaposto

Reputation: 12213

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

Upvotes: 2

Vivek
Vivek

Reputation: 4886

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

Upvotes: 1

Related Questions