This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
how to convert timeInMs to this format dd/mm/yyyy
var timeInMs = Date.now();
alert(timeInMs);
//convert to dd/mm/yyy
is there any advantage of using jquery when dealing with time/date?
var date = new Date();
var year = date.getUTCFullYear();
var month = date.getUTCMonth();
var day = date.getUTCDate();
//month 2 digits
month = ("0" + (month + 1)).slice(-2);
//year 2 digits
year = year.toString().substr(2,2);
var formattedDate = day + '/' + month + "/" + year;
prints 25/10/13
Related
This question already has answers here:
How to add days to Date?
(56 answers)
Closed 11 months ago.
So I'm getting the date to this format like this: 12.12.2023 with this function:
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = dd + '.' + mm + '.' + yyyy;
And what I want to do is for example, add more 10 days to it. for example, if it is: 12.12.2023 to be 22.12.2023
let days = 10;
today.setDate(today.getDate() + days);
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I need to convert the current timestamp (Eg: 1578293326452) to yyyy-mm-dd hh:mm:ss format
using javascript.
I obtained the current timestamp as follows:
var date = new Date();
var timestamp = date.getTime();
How can I change the format?
function getTime(){
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth() +1);
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return formateTime(year, month, day, hour, minute, second);
}
function formateTime(year, month, day, hour, minute, second){
return makeDoubleDigit(year) + "-" +
makeDoubleDigit(month) + "-" +
makeDoubleDigit(day) + " " +
makeDoubleDigit(hour) + ":" +
makeDoubleDigit(minute) + ":" +
makeDoubleDigit(second);
}
function makeDoubleDigit(x){
return (x < 10) ? "0" + x : x;
}
console.log(getTime())
Maybe this is what you need
d = Date.now();
d = new Date(d);
d = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+(d.getHours() > 12 ? d.getHours() - 12 : d.getHours())+':'+d.getMinutes()+' '+(d.getHours() >= 12 ? "PM" : "AM");
console.log(d);
This question already has answers here:
How to subtract days from a plain Date?
(36 answers)
Closed 8 years ago.
I'm trying to get the date 2 weeks before now.
this is my get today in the format I need it function:
function getTodayInFormat()
{
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getYear()-100;
return "20" + curr_year + "/" + curr_month + "/" + curr_date ;
}
Now lets say I do:
var today_date=getTodayInFormat();
How do I get two weeks before this date's date? In the same format? Thank you
Have you considered the plugin moment.js? I've used it to do things like this in the past and its fantastic and easy to use.
Create a function that you can pass a date object to, and subtract two weeks from that date, and return it in whatever format you need.
function two_weeks_ago(date_object) {
var two_weeks = 1209600000; // two weeks in milliseconds
var total = date_object.getTime() - two_weeks;
var date = new Date(total);
return date.getFullYear() +'/'+ (date.getMonth() + 1) +'/'+ date.getDate();
}
FIDDLE
This question already has answers here:
How to create a date object from string in javascript [duplicate]
(8 answers)
Closed 9 years ago.
I want to assign date to new date object. when I am using it is not giving correct result http://jsfiddle.net/Cqw5c/
new Date (12/12/2012)
function getD() {
var d = new Date("12/12/2012");
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
alert( month + "/" + day + "/" + year);
}
Update
HTML:
$('#clickme').click(function() {
var thetestdate = $('#testdate').val();
mynewdate = getD(thetestdate);
alert(mynewdate);
});
JS:
function getD(mydatevariable) {
var d = new Date(mydatevariable);
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
var wholedate = ( month + "-" + day + "-" + year);
return wholedate;
}
Quote the argument:
var d= new Date("12/12/2012");
Here's an updated fiddle. You're currently creating a date with the result of the expression 12/12/2012, which is three numbers and two division operators:
console.log(12 / 12 / 2012); // 0.0004970178926441351
You're creating a new date based on
(12 / 12) / 2012 == 0.0004970178926441351
which will be some fraction of a second after January 1, 1970.
You can create a Javascript date by parsing a string:
new Date( "12/12/2012");
or by passing in individual components:
new Date( 2012, 12, 12 );
See Date at MDN for more variations.
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I want to change YYYY-MM-DD HH:MM:SS to MM-DD-YYYY
Example:
I have a string which represents a date in this format: 2013-06-15 03:00:00
I want to change this string into 06-15-2013 using JavaScript.
Is there any library to do this? or should i just use JavaScript?
function change(time) {
var r = time.match(/^\s*([0-9]+)\s*-\s*([0-9]+)\s*-\s*([0-9]+)(.*)$/);
return r[2]+"-"+r[3]+"-"+r[1]+r[4];
}
change("2013-06-15 03:00:00");
see moment.js.. i found it very useful
http://momentjs.com/
DEMO: http://jsfiddle.net/abc123/f6k3H/1/
Javascript:
var d = new Date();
var c = new Date('2013-06-15 03:00:00');
alert(formatDate(c));
alert(formatDate(d));
function formatDate(d)
{
var month = d.getMonth();
var day = d.getDate();
month = month + 1;
month = month + "";
if (month.length == 1)
{
month = "0" + month;
}
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
return month + '-' + day + '-' + d.getFullYear();
}
Since this doesn't use RegEx you'll notice some weirdness....Examples:
d.getMonth() + 1
this is because getMonth is 0 indexed....
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
this is because hours, seconds, minutes all return 1 digit when they are 1 digit...this makes them return a leading 0. The same can be done to Month and Day if desired.