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/
Related
Using the following:
var timestart = $('.thisDiv').data("timestart");
var startDateTime = new Date(timestart);
to collect a date from a php file that is updating by ajax from this:
$TimeStart = date( 'Y,m,d,g,i', $TimeStart );
<div class="thisDiv" data-timestart="<?= $TimeStart ?>"></div>
var timestart = $('.thisDiv').data("timestart");
In console I'm getting the following when logging timestart and startDateTime:
2017,07,24,7,50
Invalid Date
If I paste the date that is output as follows
var startDateTime = new Date(2017,07,24,7,50);
Then it works fine. Any ideas why I'm getting Invalid Date?
Your timestart variable (JavaScript) is just a string. So it's a string 2017,07,24,7,50, and not those elements in order - which can't be used as separate parameters like new Date() expects.
Let's take a look at it!
var startDateTime = new Date(2017,07,24,7,50); // Parameters in order - all OK!
var startDateTime = new Date("2017,07,24,7,50"); // A single string - single parameter, not OK!
You need to return a proper format of dates from PHP with a format that's valid in JavaScript. Per the ECMAScript standard, the valid format that should work across all browsers is YYYY-MM-DDTHH:mm:ss.sssZ (see the reference at the bottom). To define that from PHP, you would need to format it as such
$TimeStart = date('c', $TimeStart);
This would return a format such as 2017-07-24T21:08:32+02:00.
Alternatively, you can use a splat/spread-operator ... and split the string it into elements, which I find as the better approach than above.
var timestart = $('.thisDiv').data("timestart"); // Get the string: "2017,07,24,7,50"
timestart = timestart.split(","); // Split into array
var startDateTime = new Date(...timestart); // Pass as arguments with splat-operator
Spread/splat operator in JavaScript
PHP date() documentation
ECMAScript date string (JavaScript)
You need to convert your date from string format to numbers.
var timestart = $('.thisDiv').data("timestart").split(",").map(Number);
var startDateTime = new Date(...timestart);
console.log(startDateTime)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thisDiv" data-timestart="2017,07,24,7,50"></div>
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;)
Creating date using dynamically generated date string throws invalid date message only in IE11.
Example
var today = new Date();
var ag_endDate = new Date(today).toLocaleDateString('en-US');
var final = new Date(ag_endDate);
console.log(final);
try this
var dateObj = new Date();
var usFormatDate = dateObj.toLocaleDateString('en-US');
console.log( usFormatDate );
sorry, my bad! date constructor does take date Object as parameter.
However, if your intention is to simply get the formatted date in en-US locale, then you don't have to create another date object for the same.
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.
Can anyone help me to convert date format 27.05.2015 01:46:32.UTC to Locale time.
I tried the following code
var date = new Date('27.05.2015 01:46:32.UTC ');
date.toString();
This is also not working.
Even momentjs is also gives error to convert this format of date.
As per statement even momentjs is also gives error to convert this format of date, I have taken liberty and used momentjs here.
You need to use string-format moment constructor to pass string and format in which input string is.
Use
var t = "27.05.2015 01:46:32.UTC";
//pass dateTime string and its format, it will return
//moment object
var cdt = moment.utc(t, 'DD.MM.YYYY HH:mm:ss');
//Get date object
var date = cdt.toDate();
//Use date variable as per requiremnt
alert(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>
The format you are using for Date is DD.MM.YYYY. So you have to change this to MM.DD.YYYY for a valid java script object.
var date = '27.05.2015 01:46:32.UTC';
var date1 = date.split(' ');
console.log(date1)
var date2 = date1[0].split('.');
console.log(date2)
var date3 = date2[1]+ '.' +date2[0] +'.'+ date2[2];
console.log(date3)
var final_date = date3 + ' ' + date1[1];
console.log(final_date);
final_date.toString();