Date Conversion in Javascript [duplicate] - javascript

This question already has answers here:
convert date from dd/mm/yyyy to yyyy-mm-dd in javascript
(4 answers)
Closed 9 years ago.
How to convert date format in Javascript like
(MM-dd-YYYY) to (YYYY-MM-DD)
please suggest some standard format for conversion

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var newDate = curr_year + "-" + curr_month + "-" + curr_date ;

This should work fine
var str = "01-17-2021";
var arr = str.split("-");
var dateFormatted= arr[2]+"-"+arr[0]+"-"+arr[1];

Look at this link.
http://jsfiddle.net/9C2p8/
var start = new Date('10-10-2013');
var startDate = $.datepicker.formatDate('yy-mm-dd', new Date(start));
alert(startDate);

Related

How to convert the date into yyyy-mm-dd this format using javascript? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I am trying to convert the date to yyyy-mm--dd this format.Here is my code .can anyone please tell me how to do?
var curr_date = new Date(date - i*86400000);
Output of the code is coming like this
Thu Jul 17 2014 10:37:44 GMT+0530 (India Standard Time)
but i need the output like year-mm-dd.
I am trying this also for year-mm-dd .this is working fine.
var date = new Date();
var date_format =(date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate());
but i want to convert
var curr_date = new Date(date - i*86400000);
to yyyy-mm--dd
how to append var date_format and var curr_date
Use FormatDate
$.datepicker.formatDate('yyyy-mm-dd', new Date(date - i*86400000));
Reference Jquery UI:
This will do the trick.
var date = new Date();
var d = new Date(date - i*86400000);
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var date_format = curr_year + "-" + curr_month + "-" + curr_date;

Converting date format from mm/dd/yyyy to yyyy-mm-dd format after entered

In my datepicker the date will be inserted in mm/dd/yyyy format. But after I inserted I want it to be sent in yyyy-mm-dd format. I am using JavaScript to do this. But I wasn't able to do that. So what should I do?
Thanks & regards,
Chiranthaka
you could also use regular expressions:
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})\/(\d{1,2})\/(\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
The expression also works for single digit months and days.
I did the opposite for my website, but it might help you. I let you modify it in order to fit your requierements. Have fun !
getDate
getMonth
getFullYear
Have fun on W3Schools
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
if(curr_month < 10)
curr_month = "0"+curr_month;
if(curr_date < 10)
curr_date = "0"+curr_date;
var curr_date_format = curr_date+"/"+curr_month+"/"+curr_year;
Adding more to Christof R's solution (thanks! used it!) to allow for MM-DD-YYYY (- in addition to /) and even MM DD YYYY. Slight change in the regex.
var convertDate = function(usDate) {
var dateParts = usDate.split(/(\d{1,2})[\/ -](\d{1,2})[\/ -](\d{4})/);
return dateParts[3] + "-" + dateParts[1] + "-" + dateParts[2];
}
var inDate = "12/06/2013";
var outDate = convertDate(inDate); // 2013-12-06
As Christof R says: This also works for single digit day and month as well.
// format from M/D/YYYY to YYYYMMDD
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear();
var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
var dd = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
return "".concat(yyyy).concat(mm).concat(dd);
};
var siku = new Date();
document.getElementById("day").innerHTML = siku.yyyymmdd();

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

How to assign date to new date object [duplicate]

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.

How to split the date , time using javascript?

How to split the date,time using java script ? I am getting date,time 2010-05-04 20:13:12.0.But i need only date.
date format mm/dd/yyyy
var cdate = gridline[i=1]["CDATE"];
Now i need cdate as mm/dd/yyyy only.
Please help me.
function getDateFromString(datestring) {
var d = new Date(datestring);
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
return curr_date + "-" + curr_month + "-" + curr_year;
}
Source: webdevelopersnotes.com

Categories

Resources