Why i am getting invalid date and NaN here? - javascript

var now = new Date;
var timenow = [now.getHours(),now.getMinutes(),now.getSeconds()].join(':');
var date1 = '2011/11/30';
var dat = new Date(date1 +timenow);
document.write(dat);
document.write(Date.parse(dat));
I am getting the date string from my service and then appending the time, why it is throwing me invalid date.

You're missing a space after the date, so you're trying to parse something like 2011/11/3013:44:02 rather than 2011/11/30 13:44:02.
var now = new Date;
var timenow = [now.getHours(),now.getMinutes(),now.getSeconds()].join(':');
var date1 = '2011/11/30 ';
var dat = new Date(date1 + timenow);
document.write(dat);
document.write(Date.parse(dat));

Try this
var now = new Date;
var timenow = [now.getHours(),now.getMinutes(),now.getSeconds()].join(':');
var date1 = '2011/11/30';
var dat = Date(date1 + timenow);
document.write(dat);
Here is the working fiddle

Because he cant parse correctly. Look this example: Example
new Date => () missing.
on var dat is empty space missing.

The date1 + timenow string, which you are feeding into the Date constructor, would look something like this:
2011/11/3014:34:53
This is not a valid date specification as per the Date.parse method (using the Date constructor like this is equivalent to calling parse), so you get an invalid result. Do this instead:
var dat = new Date(date1.replace(/\//g, '-') + "T" + timenow);
This makes your string look like 2011-11-30T14:34:53, which is a format supported by parse.

Related

Date format conversion from "yyyy-mm-dd" to "dd/mm/yyyy" in pentaho using javascript

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;)

Parse dates from Javascript Date Object to regular datetime

How do I convert a date from JS Date Object to php datetime?
The following dates were converted to a Javascript Date Object from php datetimes:
"startsAt": "2015-08-15T10:46:00+00:00",
"endsAt": "2015-08-15T11:46:00+00:00"
converted using this syntax in javascript:
for (var i = 0; i < events.events.length; i++) {
events.events[i].startsAt = new Date(events.events[i].startsAt);
events.events[i].endsAt = new Date(events.events[i].endsAt);
}
startsAt and EndsAt looked like so after the conversion:
startsAt: Date 2015-08-15T11:46:00.000Z
endsAt: Date 2015-08-15T11:46:00.000Z
Now my goal is to do the opposite in javascript (angularjs): Convert from Javascript Date Object to php datetime 2015-08-15T10:46:00+00:00. Any idea would help. Thanks
Just to fix correct an answer below:
var rawDate = new Date();
var parsedDate = [
rawDate.getDate(),
(rawDate.getMonth() + 1), //because getMonth() starts from 0
rawDate.getFullYear()
].join('-');
console.log(parsedDate); // output could be: 19-08-2015 for example
or just call any of the following to convert date object to string:
rawDate.toJSON();
rawDate.toISOString();
You've got to parse it manually doing something like:
var rawDate = new Date();
var parsedDate = rawDate.getDate() + '-' + rawDate.getMonth() + '-' + rawDate.getFullYear();
console.log(parsedDate); // output could be: 19-08-2015 for example
Other possible ways of doing this could be found in here.

Convert datetime object into specific string Javascript

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.

Javascript to format date value

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 );

Passing value to new Date giving Invalid date

When i pass value to new Date then i get Invalid date. I am doing it like
var s1 = moment("20.06.2013 09:11:00", "DD.MM.YYYY HH:mm:ss");
var s2 = s1.format("YYYY.MM.DD HH:mm:ss");
var dt1 = s2.replace(/[-,.:\s]/g, ",");
var dt2 = new Date(dt1);
In Debug mode in google chrome when i get value of dt1 as "2013,06,20,09,11,00". But when i type like new Date(2013,06,20,09,11,34) then i get date.
I also tried removing double quotes but it doesnt remove double quotes. What should i do to get rid of this error.
If you are after a Date, but want to parse using moment.js, then just do this:
var m = moment("20.06.2013 09:11:00", "DD.MM.YYYY HH:mm:ss");
var dt = m.toDate();
As Kalley suggested, use parameters in your code:
var s1 = moment([2013,06,20,09,11,34]);
Edited to add: In Jscript, moment() takes an array in the format [Y,M,D,h,m,s]. Source: http://momentjs.com/docs/

Categories

Resources