How can I convert a date value formatted as 9999-12-31T00:00:00Z to /Date(1525687010053)/ format in javascript?
I have this, but it doesn't work:
var datevalue = '9999-12-31T00:00:00Z';
var converteddate = Date.parseDate(datevalue);
I assume that you want to get the timestamp of that date. This can be achieved with the code below
var timestamp = new Date('9999-12-31T00:00:00Z').getTime()
I don't understand your question, but your code is wrong. There is no Date.parseDate() function in javascript, only Date.parse():
var datevalue = '9999-12-31T00:00:00Z';
var converteddate = Date.parse(datevalue);
document.getElementById('result').innerHTML = converteddate;
console.log(converteddate)
<p id="result"></p>
You can do your conversion in just three easy steps :
Convert your ISO 8601 string to a Date object
Use getTime to convert your Date object to a universal time timestamp
Wrap "/Date(" and ")/" around your result
Demo
function convert(iso8601string) {
return "/Date(" + (new Date(iso8601string)).getTime() + ")/";
}
console.log(convert("2011-10-05T14:48:00.000Z"));
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);
i have a start date string "20.03.2014" and i want to add 5 days to this with moment.js but i don't get the new date "25.03.2014" in the alert window.
here my javascript Code:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add("DD-MM-YYYY", 5);
alert(new_date);
here my jsfiddle: http://jsfiddle.net/jbgUt/1/
How can i solve this ?
I like this string format "25.03.2014"
Hope someone can help me.
UPDATED: January 19, 2016
As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)
var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');
Thanks #Bala for the information.
UPDATED: March 21, 2014
This is what you'd have to do to get that format.
Here's an updated fiddle
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(day + '.' + month + '.' + year);
ORIGINAL: March 20, 2014
You're not telling it how/what unit to add. Use -
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:ss'))
has to format and then convert to moment again.
The function add() returns the old date, but changes the original date :)
startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days');
alert(new_date);
You can add days in different formats:
// Normal adding
moment().add(7, 'days');
// Short Hand
moment().add(7, 'd');
// Literal Object
moment().add({days:7, months:1});
See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/
var end_date = moment(start_date).clone().add(5, 'days');
If we want to use the current date or present date:
var new_date = moment(moment(), "MM-DD-YYYY").add(7, 'days')
alert(new_date);
To get an actual working example going that returns what one would expect:
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
window.console.log(thing)
add https://momentjs.com/downloads/moment-with-locales.js to your html page
var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date
then
var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..
point 2 and 3 are using in your jquery code...
If you do end up running with formatting problems after adding X time to the function, try this format:
startDate = moment(startDate).add(1, "days").format("YYYY-MM-DD");
instead of:
startDate = moment(startDate, "YYYY-MM-DD").add(1, "days");
This last version keeps the time attached to the returned data, whereas the format method doesn't and literally returns YYYY-MM-DD.
You can reduce what they said in a few lines of code:
var nowPlusOneDay = moment().add('days', 1);
var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');
alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
updated:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');
alert(new_date)
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