Hello I have a function that generates the date with this format:
MM-DD-YYYY
Is there any jquery or javascript trick to convert that value into:
YYYY-MM-DD?
More Detailed Explanation:
The function I have generates the date and stored in a variable called tdate
So var tdate = 01-30-2001
I would like to do some jquery or javascript to turn tdate into:
tdate = 2001-01-30
tdate is a string
Thanks!
You can use .split(), destructuring assignment, termplate literal to place yyyy, mm, dd in any order
var date = "01-30-2001";
var [mm, dd, yyyy] = date.split("-");
var revdate = `${yyyy}-${mm}-${dd}`;
console.log(revdate)
You can use a little bit regex to capture year, month and day and reorder them:
var tdate = "01-30-2001";
console.log(
tdate.replace(/^(\d{2})-(\d{2})-(\d{4})$/, "$3-$1-$2")
)
Can slice() up the string and put it back together the way you want it
var tdate = '01-30-2001';
tdate = [tdate.slice(-4), tdate.slice(0,5)].join('-');
// or tdate = tdate.slice(-4) + '-' + tdate.slice(0,5)
console.log(tdate)
you can split the string on '-' and then re arrange the array once and join again to form the date.
var date = "01-30-2001";
var arr = date.split("-");
var revdate = arr.splice(-1).concat(arr.splice(0,2)).join('-');
console.log(revdate);
Related
I have a date displaying on my website in this format:
MM/DD/YY
I would like to convert it to the following format using jQuery:
YYYY-MM-DD
I would prefer to do it without a plugin. I was able to replace the forward slashes with hyphens using the code below but now I am stuck.
var date = "05/23/21";
var formattedDate = date.replace(/\//g, '-');
$('body').append(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
To do what you require you can convert the input string to a Date object, then use the methods JS exposes to pull out the constituent parts in to the format you require:
let input = "05/23/21";
let date = new Date(input);
const zeroPad = (num, places) => String(num).padStart(places, '0')
let formattedDate = `${date.getFullYear()}-${zeroPad(date.getMonth() + 1, 2)}-${zeroPad(date.getDate(), 2)}`;
$('body').append(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
This method assumes all dates are 20XX. It splits the date by /, reverses that array (getting the YY MM DD format), then reformats the year to be YYYY and joins the array back together with -
var date = "05/23/21";
var formattedDate = date.split('/').reverse().map((e, i) => i === 0 ? `20${e}` : e).join('-');
$('body').append(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
By using regex group and replace it with reverse order you can do it.
var date = "05/23/21";
const formattedDate = date.replace(/(\d+)\/(\d+)\/(\d+)/, "20$3-$2-$1");
console.log(formattedDate);
.as-console-wrapper{min-height: 100%!important; top: 0}
Please check the following code. Let me know if something goes wrong
const d = date.split("/");
var res = '20'+d[2] +'-'+d[0]+'-'+d[1];
I need to get a date in this format:
2016-07-06T10:57Z
Using this code I have been able to get a date in a format somewhat like I need:
var isoDate = new Date().toISOString();
2016-07-06T08:46:08.127Z
But is there a way I can remove the seconds and fraction of seconds from the date so it appears exactly like the date: "2016-07-06T10:57Z" ?
You will always want to remove the last 8 characters ('Z' included) thus you can use a function like slice
isoDate = isoDate.slice(0, -8); //Remove seconds + fractions + Z
isoDate += "Z"; //Add back the Z
You can use this way because the format returned by toISOString() will always be
YYYY-MM-DDTHH:mm:ss.sssZ
Please try
var isoDate = new Date().toISOString();
var pos = isoDate.lastIndexOf(':');
var datePart1 = isoDate.substring(0,pos);
var datePart2 = isoDate.substr(-1, 1);
var dateStr = datePart1+datePart2;
console.log(dateStr);
I have a variable which stores the date as "18/07/2013". I need to parse this to "07/18/2013". How this is possible in jquery?
var date = $("#dte").val();
var date = $("#dte").val();
d=date.split('/');
newdate=d[2]+"/"+d[1]+"/"+d[0];
return newdate;
You could use
var date = $("#dte").val().replace(/^(\d\d)\/(\d\d)/, "$2/$1");
to swap month and date part.
A simple replace would do the trick:
date = date.replace(/^(\d\d)\/(\d\d)\//, "$2/$1/");
No need of jQuery for this.
Try this:
var date = $("#dte").val().split('/');
var newDate = date[1]+'/'+date[0]+'/'+date[2];
alert(newDate); // or $("#dte").val(newDate); if you want to update the input
Try this out.
It helped me to format date in jQuery
$.datepicker.formatDate('dd/mm/yy', new Date(yourDateString))
There is jQuery dateFormat jquery plugin..you can use that for show date format.
var date = date.replace(/(\d\d)\/(\d\d)\/(\d\d)/, "$2/$1/$3");
JSFIDDLE
javascript's Date accepts a string in the format year / month / day, so just reverse the order and create a date object :
var date = '18/07/2013';
var dObj = new Date(date.split('/').reverse().join('/'));
var newD = dObj.getDate() + '/' + (dObj.getMonth()+1) + '/' + dObj.getFullYear();
FIDDLE
Is there a way I could get the year, month (0 based) and day from '03/05/2013'
If so, how?
Thanks
Is there a safe way to do it that can check if it is in the correct format?
You have the Date.parse method which parses a Date string and returns its timestamp, so you can call new Date().
Something like this:
new Date(Date.parse('03/06/2013'))
Most easy is using the split() function, i think:
var date = "03/05/2013";
var dateParts = date.split("/");
var day = dateParts[0];
var month = dateParts[1];
var year = dateParts[2];
http://jsfiddle.net/s7ma2/1/
I have inserted random date into a textbox let it be 12/2/2009 now I want only 2 in this how to do it with javascript how do I need array or any other method.
Please give me an appropriate example
Use :
var mydate = $("textboxId").val();
var dateArr = mydate.split("/");
var date = dateArr[0];
var month = dateArr[1];
var year = dateArr[2];
You'll get month from 'dd/m/yyyy' formatted date by reversing the date
new Date("12/2/2009".split("/").reverse().join("/")).getMonth()
Check here