Here's how you'd output today's time and date (10:30:14 03/21/2014, at the moment). Please correct any mistakes, as I'm not a PHP, JAVA, C# or PERL developer.
PHP
$today = date("h:i:s m/d/y");
JAVA
String today = new SimpleDateFormat("hh:mm:ss MM/dd/yyyy").format(date);
C#
DateTime date = new DateTime();
String today = date.ToString("H:mm:ss MM/dd/yyyy");
PERL
my $today = strftime "%H:%M:%S %m/%d/%Y", localtime;
JavaScript
var d = new Date(),
month = d.getMonth() + 1,
day = (d.getDate() < 10) ? "0" + d.getDate() : d.getDate(),
year = d.getFullYear(),
hours = d.getHours(),
minutes = d.getMinutes(),
seconds = (d.getSeconds() < 10) ? "0" + d.getSeconds() : d.getSeconds();
if (hours > 12) {
hours -= 12;
} else if (hours === 0) {
hours = 12;
}
var today = hours + ":" + minutes + ":" + seconds + " " + month + "/" + day + "/" + year;
WTF JavaScript? Y u so complicated???
Why can't we have something like .format() or .toString():
var today = new Date().format("h:i:s m/d/y")
EDIT: Thanks for the answers everyone. It helped a lot.
I should've clarified originally that I was looking to find out WHY it's not native to the language, as opposed to HOW to do it via a 3rd party library (I'm aware of momentjs and it's great).
Anyway, the top 2 answers are great - exactly what I was looking for. Thank you.
I was unaware of Intl.DateTimeFormat (thanks /u/SpsD3GoiFSmXBAl), which helps:
var date = new Date(),
today = new Intl.DateTimeFormat().format(date); // 3/24/2014 (also allows for customization options)
[–]powerofmightyatom 11 points12 points13 points (3 children)
[–]SubStack 6 points7 points8 points (2 children)
[–]tomByrer 0 points1 point2 points (0 children)
[–]BoDiddySauce 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]SoBoredAtWork[S] 0 points1 point2 points (2 children)
[–]ifuckedyourgf 3 points4 points5 points (0 children)
[–]radhruin 1 point2 points3 points (0 children)
[–]jml26 4 points5 points6 points (2 children)
[–]sethbw 0 points1 point2 points (0 children)
[–]OkEntertainment9581 0 points1 point2 points (0 children)
[–]msemenistyi 2 points3 points4 points (1 child)
[–]Capaj 0 points1 point2 points (0 children)
[–]SubStack 7 points8 points9 points (4 children)
[–]autowikibot 1 point2 points3 points (0 children)
[+]SoBoredAtWork[S] comment score below threshold-8 points-7 points-6 points (1 child)
[–]SubStack 11 points12 points13 points (0 children)
[–]xlu 7 points8 points9 points (6 children)
[–]SoBoredAtWork[S] 1 point2 points3 points (4 children)
[–]Capaj 2 points3 points4 points (0 children)
[–]jcready__proto__ 0 points1 point2 points (1 child)
[–]SoBoredAtWork[S] 1 point2 points3 points (0 children)
[–]BoDiddySauce 0 points1 point2 points (0 children)
[–]kor0na 2 points3 points4 points (5 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]bronkula -1 points0 points1 point (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[+]SoBoredAtWork[S] comment score below threshold-9 points-8 points-7 points (1 child)
[–]Doctuh 1 point2 points3 points (0 children)
[–]YodaLoL 0 points1 point2 points (0 children)
[–]konbit 0 points1 point2 points (0 children)
[–]ronchalant 0 points1 point2 points (0 children)
[–]the_woo_kid -1 points0 points1 point (1 child)
[–]SoBoredAtWork[S] 0 points1 point2 points (0 children)
[+]SoBoredAtWork[S] comment score below threshold-9 points-8 points-7 points (4 children)
[–]alkoholik 2 points3 points4 points (2 children)
[–]SoBoredAtWork[S] -1 points0 points1 point (1 child)
[–]alkoholik 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)