I am trying (and failing!) to format my date output using javascript, so I am asking for some help.
The date format is 2013-12-31 (yyyy-mm-dd), but I want to display the date as 12-2013 (mm-yyyy).
I have read many posts, but I am now just confused.
Here is the call to the javascript function:
changeDateFormat($('#id_award_grant_date').val()),
Here is the javascript function:
function changeDateFormat(value_x){
}
What you have is just a string, so just split it and put it back together in the wanted format
var date = '2013-12-31',
parts = date.split('-'),
new_date = parts[1]+'-'+parts[0];
FIDDLE
var d = '2013-12-31'
var yyyymmdd = d.split('-')
var mmyyyy = yyyymmdd[1]+'-'+yyyymmdd[0]; // "12-2013"
function changeDateFormat(str) {
var reg = /(\d+)-(\d+)-(\d+)/;
var match = str.match(reg);
return match[2] + "-" + match[1];
}
http://jsfiddle.net/wZrYD/
You can do this pretty easily with split and join.
var yyyymmdd = '2013-12-31'.split('-'),
mmyyyy = [ yyyymmdd[1], yyyymmdd[0] ].join('-');
console.log('The date is: ', mmyyyy );
Related
I am trying to convert the "20150812" to "12-08-2015".
my returned date object shown below:
study response = [
{
"dob": {
"Value": [
"20151208"
]
}
}
]
javascript function
var dob = study["dob"]["Value"]; //returning 20151208
expected output
//08-12-2015
tried the following :
Date.parse(dob) ; //return NaN
Any help appreciated.
I am trying to convert the "20150812" to "12-08-2015"
Since dob is a text string, and you want a text string result, this is probably easiest to do with text string manipulation, like this:
// var dob = study["dob"]["Value"]; //returning 20151208
var dob = "20151208";
var converted = dob.replace(/^(\d\d\d\d)(\d\d)(\d\d)$/, "$2-$3-$1");
console.log(converted);
The output will match your expected output.
You can use moment js to manipulate dates. Read more : Moment.js
below code converts your string date which is in format YYYYDDMM to DD-MM-YYYY
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script>
var mydate = 20151208;
var str = moment(20151208,"YYYYDDMM").format('DD-MM-YYYY');//You need this line
console.log(str);
</script>
You can also use take substrings to get day,month and year
var str = "20151208";
var year = str.substr(0, 4);
var day = str.substr(4, 2);
var month= str.substr(6,2);
console.log( day+"-"+month+"-"+year);
I have a csv file where the date field has a format "yyyy-mm-dd" and I wish to convert it into "dd/mm/yyyy" using javascript. This is the javascript it found out from this reference
"could not apply the given format yyyy/mm/dd on the string for 2015-02-04 :Format.parseObject(String) failed(script#3)"
this is the javascript code I used
var dateObj = str2date(Date_of_joining, "yyyy/mm/dd");
var newDate = date2str(dateObj, "dd/MM/yyyy");
I even tried using Select Value step and changed the meta data to date and specified the format to "dd/MM/yyyy" but still not working.How do I solve this
The date you are parsing is not using slashes, but you're defining slashes when you parse it. Switch your slashes to dashes:
var dateObj = str2date(Date_of_joining, "yyyy-mm-dd");
var newDate = date2str(dateObj, "dd/MM/yyyy");
function convertLinuxDate(linux_date) {
//linux_date = "2001-01-02"
var arrDate = linux_date.split("-");
return arrDate[1] + "/" +arrDate[2] + "/" + arrDate[0];
}
//returns 01/02/2001
Here we go:
Try to reconstruct DateTime string as like this:
var dateObj = new Date(Date_of_joining);
var newDate = new Date(dateObj );
var formattedString = [newDate.Date(),newDate.Month()+1, newDate.getFullYear()].join("/");
alert(formattedString );
Hope it helps;)
Sample JSON:
[{"DispatchDt":"2014-05-28T01:34:00","RcvdDt":"1988-12-26T00:00:00"}]
I have this set of dates and I want to convert it to the date format (mm/dd/yyyy). How do you do this is JavaScript?
Unfortunately parsing and formatting dates is a weak side of javascript.
So usually for that we use the 3rd party libraries like moment.js
An example of how you would do that with moment.js:
var date = '2014-05-28T01:34:00';
var parsedDate = moment(date);
var formattedDate = parsedDate.format('MM/DD/YYYY');
Demo: http://jsfiddle.net/8gzFW/
You may format the json string into new Date() then use js methods to get month,day,year or what exactly you need.
//format string to datetime
var DispatchDt = new Date(this.DispatchDt);
// then use this methods to fetch date,month, year needed
//getDate() // Returns the date
//getMonth() // Returns the month
//getFullYear() // Returns the year
var DispatchDt_date = DispatchDt.getDate();
var DispatchDt_month = DispatchDt.getMonth() + 1; //Months are zero based
var DispatchDt_year = DispatchDt.getFullYear();
Sample on jsFiddle
This would work
var a = "[{"DispatchDt":"2014-05-28T01:34:00","RcvdDt":"1988-12-26T00:00:00"}]";
var b = JSON.parse(c);
for (i in b[0]) {
b[0][i] = b[0][i].slice(0, 9)
}
console.log(b); // b now looks like this [ { DispatchDt: "2014-05-28", RcvDt: "1988-12-26" } ]
Please note the dates are stringified.
I have a date in the format dd.MM.yyyy HH:mm, for example 31.01.2014 11:24. How can I obtain, in javascript, the string "/Date(1391160281569)/"?
Here is an approach
var date = "31.01.2014 11:24";
var sp1 = date.split(/[:. ]/);
var newDate = new Date(sp1[2], (+sp1[1] - 1), sp1[0], sp1[3], sp1[4]);
var milliSeconds = newDate.getTime();
var urFormat = "/Date(" + milliSeconds + ")/";
alert(urFormat)
JSFiddle
I took me a while but i got this:
var theStringVersion = "/Date("+$.now()+")/";
Of course, for a real date, i would have to get the timestamp for it.
I am getting this "20131218" date/time value from an API result.
What I want to do is convert this date into something like this "2013-12-18". I know this is very easy in PHP by simply doing this code:
echo date("Y-m-d",strtotime('20131218'));
output: 2013-12-18
This is what I tried in javascript:
var myDate = new Date("20131218");
console.log(myDate);
But the output is Date{ Invalid Date } so obviously this is wrong.
My question here what is the equivalent of strtotime in javascript? or if there's no equivalent, how would I convert this value as my expected result(2013-12-18) using javascript?
Your help will be greatly appreciated!
Thanks! :)
The value is invalid to convert it to date. So either from your PHP code send it as a proper format like 20131218
Or convert the value you get in your Javascript to similar kind of format.
var dateVal="20131218";
/*
// If it's number ******* //
var numdate=20131218;
var dateVal=numdate.toString();
*/
var year=dateVal.substring(0,4);
var mnth=dateVal.substring(4,6);
var day=dateVal.substring(6,8);
var dateString=year+"-"+mnth+"-"+day;
var actualDate = new Date(dateString);
alert(actualDate);
JSFIDDLE DEMO
Javascript has a Date.parse method but the string you have is not suitable to pass to it. You don't really need to create a date object just to format a string. Consider:
function formatDateStr(s) {
s = s.match(/\d\d/g);
return s[0] + s[1] + '-' + s[2] + '-' + s[3];
}
alert(formatDateStr('20131218')); // '2013-12-18'
If you wish to convert it to a date object, then:
function parseDateStr(s) {
s = s.match(/\d\d/g);
return new Date(s[0] + s[1], --s[2], s[3]);
}
The reason why it is showing Invalid date is, it wants it to be in format
Following format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS
If you breakdown your string using following format just add dash at relevant places then you are good to go and use newDate.
1. var myDate = new Date("2013-12-18");
alert(myDate);
2. var myDate = new Date(2013,12,18);
Eventually you can modify your string manipulate it and use it in aforementioned format.