how to parse a dd/mm/yyyy or dd-mm-yyyy or dd-mmm-yyyy formatted date string using JavaScript or jQuery
You might want to use helper library like http://momentjs.com/ which wraps the native javascript date object for easier manipulations
Then you can do things like:
var day = moment("12-25-1995", "MM-DD-YYYY");
or
var day = moment("25/12/1995", "DD/MM/YYYY");
then operate on the date
day.add('days', 7)
and to get the native javascript date
day.toDate();
How to convert dd/mm/yyyy string into JavaScript Date object?
MM/DD/YYYY format
If you have the MM/DD/YYYY
format which is default for JavaScript, you can simply pass your string to Date(string)
constructor. It will parse it for you.
var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();
How to convert a dd.MM.yyyy string to a Date object
If you absolutely have to have it in that format, you can rearrange it quick before using the date object as it expects a certain format..
Something like this could work.
if( typeof myVar === 'string') {
let dateArr = myVar.split('.');
let myDate = new Date(dateArr[1] + '-' + dateArr[0] + '-' + dateArr[2]);
if (myDate.getTime() < this.getMinDate().getTime()) /** compare two dates */
//omitted source code
}
How To Convert String ' dd/mm/yy hh:MM:ss ' to Date in javascript?
If you are looking for alternatives in jquery or Javascript , then you can go with Moment.js,where you can Parse, Validate, Manipulate, and Display dates in JavaScript.
example:
var date= moment("06/06/2015 11:11:11").format('DD-MMM-YYYY');
Date format conversion from dd-MMM-yyyy to dd-MM-yyyy in javascript
Here is the Answer...
function GetDate(str) {
debugger;
var arr = str.split('-');
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
var i = 1;
for (i; i <= months.length; i++) {
if (months[i] == arr[1])
{
break;
}
}
var formatddate = i + '/' + arr[0] + '/' + arr[2];
return formatddate;
}
function StartDateTimeEndDate() {
var startDate = document.getElementById('<%=EFF_START_DATEDVTextBox.ClientID %>').value
var endDate = document.getElementById('<%=EFF_END_DATEDVTextBox.ClientID %>').value
var startdt = GetDate(startDate)
var enddt = GetDate(endDate)
if ((Date.parse(enddt) <= Date.parse(startdt))) {
alert("End date should be greater than Start date");
document.getElementById('<%=EFF_END_DATEDVTextBox.ClientID %>').value = "";
}
}
How to get current formatted date dd/mm/yyyy in Javascript and append it to an input
I hope this is what you want:
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = dd + '/' + mm + '/' + yyyy;
document.getElementById('DATE').value = formattedToday;
How do I get the current date in JavaScript?
How to format date in javascript as DD-MMM-YYYY?
Thanks for all the contributions to this question.
I managed to create a working function for this:
formatDate(value) {
let date = new Date(value);
const day = date.toLocaleString('default', { day: '2-digit' });
const month = date.toLocaleString('default', { month: 'short' });
const year = date.toLocaleString('default', { year: 'numeric' });
return day + '-' + month + '-' + year;
}
Format JavaScript date as yyyy-mm-dd
You can do:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
Get current date in DD-Mon-YYY format in JavaScript/Jquery
There is no native format in javascript for DD-Mon-YYYY
.
You will have to put it all together manually.
The answer is inspired from :
How do I format a date in JavaScript?
// Attaching a new function toShortFormat() to any instance of Date() class
Date.prototype.toShortFormat = function() {
const monthNames = ["Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"];
const day = this.getDate();
const monthIndex = this.getMonth();
const monthName = monthNames[monthIndex];
const year = this.getFullYear();
return `${day}-${monthName}-${year}`;
}
// Now any Date object can be declared
let anyDate = new Date(1528578000000);
// and it can represent itself in the custom format defined above.
console.log(anyDate.toShortFormat()); // 10-Jun-2018
let today = new Date();
console.log(today.toShortFormat()); // today's date
Related Topics
How to Map More Than One Property from an Array of Objects
Variable Scope in D3 JavaScript
Load Jquery with JavaScript and Use Jquery
Understanding Prototypal Inheritance in JavaScript
Using JavaScript to Display a Blob
Jquery Selectors on Custom Data Attributes Using HTML5
How to Refresh an Iframe Using JavaScript
Jquery.Inarray(), How to Use It Right
Why Is This Jquery Click Function Not Working
Cloud Firestore Case Insensitive Sorting Using Query
Cross-Origin Read Blocking (Corb)
My Very Simple Greasemonkey Script Is Not Running
Download and Open PDF File Using Ajax
Get Viewport/Window Height in Reactjs
Does Never Resolved Promise Cause Memory Leak