I want to get the date and time in the following format:
yyyy.mm.dd.hh.mm.ss | 2014.11.6.20.31.24
However, my code (based on Get Current Time) is instead providing these values:
y??.m?.d?.hh.mm.ss | 114.10.4.20.31.24
Here is my code:
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getYear() + "." + dt.getMonth() + "." + dt.getDay();
alert(date + "." + time);
Can someone please let me know why these odd values are in there 114.10.4 and how to change them to be what I want?
That is because you need to use
.getFullYear() for the full year
the .getMonth() is 0-based so you need to add 1
the function to get the day of month is .getDate(). The .getDay() is for the day of the week.
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + (dt.getMonth()+1) + "." + dt.getDate();
alert(date + "." + time);
If, for some weird reason, you are going only for firefox, you can use
var d = new Date(),
formatted = d.toLocaleFormat('%Y.%m.%d.%H.%M.%S');
alert(formatted);
Finally, you can use the great moment.js library and do
var formatted = moment().format('YYYY.MM.DD.HH.mm.ss');
You are using the wrong getters. Use getFullYear() instead of getYear(), and getDate() instead of getDay(). And add 1 to the month, because it starts at 0.
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + (dt.getMonth() + 1) + "." + dt.getDate();
alert(date + "." + time);
Just make sure that you are using methods what you want to use e.g:
dt.getYear() => dt.getFullYear()
For further reference see this.
should use getFullYear() instead of getYear() and getMonth() + 1 instead of getMonth() because it calculate form 0..11 and info about getDay()
var dt = new Date();
var time = dt.getHours() + "." + dt.getMinutes() + "." + dt.getSeconds();
var date = dt.getFullYear() + "." + dt.getMonth() + 1 + "." + dt.getDate();
alert(date + "." + time);
dt.getDay() this day of the week
The getDay() method returns the day of the week (from 0 to 6)
You need to use getDate() to know the number of the day (from 1 to 31)
Also, you need to add 1 to getMonth() because months in JavaScript starts on 0
Related
I used a plugin called jQRnageSlider and tried to get the date and time label showed in the slider, but when I scroll back the slider to last year, the month 12 turns out to be 11 eventually.
Normal case if I don't scroll back to last year.
It jumped to Nov 2016 immediately
That should be the issue of date formatting issue. Can anyone help?
$(".date-range-slider").dateRangeSlider({
...
formatter: function(val){
var days = ('0' + val.getDate()).slice(-2),
month = ('0' + val.getMonth() + 1).slice(-2),
year = val.getFullYear(),
hour = ('0' + val.getHours()).slice(-2),
min = ('0' + val.getMinutes()).slice(-2);
return days + "-" + month + "-" + year + " " + hour + ":" + min;
}
});
The problem is that line:
month = ('0' + val.getMonth() + 1).slice(-2)
You want to sum it mathematically not as string, so you should do:
month = ('0' + (val.getMonth() + 1)).slice(-2)
Check the snippet to see what your code is actually returning:
var val = new Date();
document.write('0' + val.getMonth() + 1)
And what returns corrected version:
var val = new Date();
document.write('0' + (val.getMonth() + 1))
I have the following value:
javascript variable:
"2015-10-14T17:54:19.033"
I want to end up with
mm/dd/yyyy
e.g.
10/14/2015
I was trying to do
var date = month[d.getMonth()] + " " + d.getDay()+ ", " + d.getFullYear();
Here is my Fiddle
http://jsfiddle.net/bthorn/7ptcedt4/
var d = new Date('2015-10-14T17:54:19.033');
var date = d.getMonth().toString() + "/" + d.getDay().toString() + "/" + d.getFullYear().toString();
console.log(date);
Something is NOT right: I am getting
9/3/2015
You can use this:
var date = new Date("2015-10-14T17:54:19.033");
var formatedDateString = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
Or use moment.js
var formatedDateString = moment("2015-10-14T17:54:19.033").format('MM/dd/YYYY');
Or may you consider create your own date format function.
PS.: javascript months are 0-indexed, so for the correct month you should use (date.getMonth() + 1)
All I want to do is to add 20 days to the current date. I need the results in mm/dd/yyyy format. Today is 05/15/2014 but this displays 05/35/2014 which of course is not a valid date.
var myDate = new Date();
alert((myDate.getMonth() + 1) + "/" + (myDate.getDate() + 20) + "/" + myDate.getFullYear());
Use the setDate() method of your Date object, passing the current date plus the number of days to add.
var myDate = new Date();
myDate.setDate(myDate.getDate() + 20);
alert((myDate.getMonth() + 1) + "/" + (myDate.getDate()) + "/" + myDate.getFullYear());
Outputs
6/4/2014
var d = new Date();
var today_date = d.getDate() + '/' + month_name[d.getMonth()] + '/' + d.getFullYear();
This is how I am getting a date. It works with a slight problem. For todays date 7th of June 2011 it returns 7/11/2011, what i want it to return is 07/11/2011?
Anyone know how?
Well, you could simply check the length of d.getDate()and if it's 1 then you add a zero at the beginning. But you would like to take a look at format() to format your dates?
Like so:
("0"+1).slice(-2); // returns 01
("0"+10).slice(-2); // returns 10
Complete example:
var d = new Date(2011,1,1); // 1-Feb-2011
var today_date =
("0" + d.getDate()).slice(-2) + "/" +
("0" + (d.getMonth() + 1)).slice(-2) + "/" +
d.getFullYear();
// 01/02/2011
Try this (http://blog.stevenlevithan.com/archives/date-time-format):
var d = new Date();
d.format("dd/mm/yyyy");
Try this, this is more understandable.:
var currentTime = new Date();
var day = currentTime.getDate();
var month = currentTime.getMonth() + 1;
var year = currentTime.getFullYear();
if (day < 10){
day = "0" + day;
}
if (month < 10){
month = "0" + month;
}
var today_date = day + "/" + month + "/" + year;
document.write(today_date.toString());
And result is :
07/05/2011
For using the Amazon mechanical turk API I want to get the current GMT time and show it in ISO format
2011-02-24T20:38:34Z
I am wondering if there is any way to correctly get the gmt time and also be able to reformat it with ISO format. I can use something like now.toGMTString(); but it makes a string out of the date and it is hard to reformat it with ISO.
var year = now.getUTCFullYear()
var month = now.getUTCMonth()
var day= now.getUTCDay()
var hour= now.getUTCHours()
var mins= now.getUTCMinutes()
var secs= now.getUTCSeconds()
var dateString = year + "-" + month + "-" + day + "T" + hour + ":" + mins + ":" + secs + "Z"
You should be using UTC now instead of GMT. (Amounts to almost the same thing now, and it is the new standard anyway)
I believe this will work for you:
Number.prototype.pad = function(width,chr){
chr = chr || '0';
var result = this;
for (var a = 0; a < width; a++)
result = chr + result;
return result.slice(-width);
}
Date.prototype.toISOString = function(){
return this.getUTCFullYear().pad(4) + '-'
+ this.getUTCMonth().pad(2) + '-'
+ this.getUTCDay().pad(2) + 'T'
+ this.getUTCHours().pad(2) + ':'
+ this.getUTCMinutes().pad(2) + ':'
+ this.getUTCSeconds().pad(2) + 'Z';
}
Usage:
var d = new Date;
alert('ISO Format: '+d.toISOString());
Not much more different than every else's answer, but make it built-in to the date object for convenience
function pad(num) {
return ("0" + num).slice(-2);
}
function formatDate(d) {
return [d.getUTCFullYear(),
pad(d.getUTCMonth() + 1),
pad(d.getUTCDate())].join("-") + "T" +
[pad(d.getUTCHours()),
pad(d.getUTCMinutes()),
pad(d.getUTCSeconds())].join(":") + "Z";
}
formatDate(new Date());
Output:
"2011-02-24T21:01:55Z"
This script can take care of it
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'}
var d = new Date();
document.write(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z