How to assign date to new date object [duplicate] - javascript

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.

Related

d.setDate(d.getDate()+7) returns a bunch of numbers [duplicate]

This question already has answers here:
setDate() returns number 1603240915215 instead of a date
(2 answers)
setDate method within another var returning a different value
(1 answer)
Closed 2 years ago.
When i run the code bellow, which parses the current date + 7 days, returns huge number on the date variable;
let d = new Date();
let n = d.setDate(d.getDate()+7);
let m = d.getMonth()+1;
let o = d.getFullYear();
let dateOp = n + "/" + m + "/" + o;
dateOp;
// returns "1609772260625/1/2021"
From the docs, setDate returns the millisecond difference between the date and the UNIX epoch. So, set the date first, then get the date again:
d.setDate(d.getDate() + 7);
let n = d.getDate();
let m = d.getMonth()+1;
let o = d.getFullYear();
let dateOp = n + "/" + m + "/" + o;
dateOp;

Convert timestamp to yyyy-mm-dd hh:mm:ss format using javascript [duplicate]

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

2 weeks before now in jquery [duplicate]

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

date now javascript convert format [duplicate]

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

javascript add month to date [duplicate]

This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Closed 9 years ago.
I want to add 1 Month or 6 Month to a given Date.
But if i add one Month, the year isnt incremented. And if i add 6 Month to June, i got the Month 00 returned BUT the year is incremented.
Could you please help me out?
function addToBis(monthToAdd){
var tmp = $("#terminbis").val().split('.');
var day = tmp[0];
var month = tmp[1];
var year = tmp[2];
var terminDate = new Date(parseInt(year),parseInt(month), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
day = "";
month = "";
year = "";
if(terminDate.getDate() < 10){
day = "0"+terminDate.getDate();
} else{
day = terminDate.getDate();
}
if(terminDate.getMonth() < 10){
month = "0"+terminDate.getMonth();
} else{
month = terminDate.getMonth();
}
year = terminDate.getFullYear();
$("#terminbis").val(day+"."+month+"."+year);
}
getMonth returns a number from 0 to 11 which means 0 for January , 1 for february ...etc
so modify like this
var terminDate = new Date(parseInt(year),parseInt(month - 1), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);
and
month = terminDate.getMonth() + 1;
You should use the javascript Date object's native methods to update it. Check out this question's accepted answer for example, it is the correct approach to your problem.
Javascript function to add X months to a date
The function can be written much more concisely as:
function addToBis(monthToAdd){
function z(n) {return (n<10? '0':'') + n}
var tmp = $("#terminbis").val().split('.');
var d = new Date(tmp[2], --tmp[1], tmp[0]);
d.setMonth(d.getMonth() + monthToAdd);
$("#terminbis").val(z(d.getDate()) + '.' + z(d.getMonth() + 1)
+ '.' + d.getFullYear();
}
The value of terminbis and monthToAdd should be validated before use, as should the date generated from the value.

Categories

Resources