I have an input value of a time specifically formatted HH:MM is there way I can convert it to a javascript valid date for example:
Time is: 10:20
Valid javascript time would be: Mon Aug 15 2016 10:20:00
var timeString = "10:20:51";
var timeStringArray = timeString.split(':');
var hours = timeStringArray[0];
var minutes = timeStringArray[1];
var seconds = timeStringArray[2];
var now = new Date();
now.setHours(hours);
now.setMinutes(minutes);
now.setSeconds(seconds);
console.log(now);
This just changes the hours/minutes component of the current time though
Related
I have 2 DateTime field in a form, and I want the difference between these 2 fields in minute.
I tried to parse DateTime into Date but it's not working :
<script>
$(document).ready(function () {
$("#mybundle_evenement_button").click(function () {
var field1 = $("#mybundle_evenement_debut").val();
var field2 = $("#mybundle_evenement_fin").val();
var date1 = new Date(field1);
var date2 = new Date(field2);
alert(date1);
});
});
</script>
If I alert() date1, it shows Invalid Date.
But if I alert() field1, it shows 15/09/2017 13:32 (format is : days/months/year hour:minutes).
Is it possible that new Date(field1) isn't working because of the format ?
I know that if I succeed to parse DateTime into Date, it'll be easy to have the difference in minutes, but I don't understand why it says Invalid Date.
dd/MM/yyyy HH:mm isn't a valid date format for Date.parse()
You have to format your date to a valid Date Time String Format, for example:
var field1 = $("#mybundle_evenement_debut").val();
var ISODate1 = field1.replace(/(\d+)\/(\d+)\/(\d+)/, "$3-$2-$1")
var date1 = new Date(ISODate1);
alert(date1) // => Fri Sep 15 2017 13:32:00 ...
The problem is about the format you are getting the date from the field. new Date() don't accepts this format. I think the best is to parse the string yourself. If the format is always the same just use new Date(year, month, day, hours, minutes, seconds, milliseconds).
var day = field.splice(0,2); field.splice(0,1);
var month = field.splice(0,2); field.splice(0,1);
var year = field.splice(0,2); field.splice(0,1);
var hour = field.splice(0,2); field.splice(0,1);
var minute = field.splice(0,2);
It's depend on your browser. I'll suggest to use the standard format is '2013/12/09 10:00'.
Okay! come to the point. You need to manually format the date from my latest answer regarding this same kind of issue. Please take a look at this link : Stange javascript Date behaviour on particular dates
And you could try this below code for getting the date difference in minutes.
var startTime = new Date('2013/12/09 10:00');
var endTime = new Date('2014/12/09 10:00');
var difference = endTime.getTime() - startTime.getTime();
var result = Math.round(difference / 60000);
alert(result);
I'm trying to set my Date object in javascript, but I keep getting wrong date. This is how I set
var today = new Date();
var year = today.getFullYear();
var month = today.getMonth();
var day = today.getDate();
var hour = today.getHours();
var minute = today.getMinutes();
var second = today.getSeconds();
var create_date = new Date(year,month,day,0,0,0).toISOString();
My local datetime is 2015-11-17 21:00:00, however this creates 2015-11-16 22:00:00.000Z. If I use hour, minute and second variables in date constructor it creates the correct time in mongodb. I want to set date as 2015-11-17 00:00:00. What might be the problem?
Thank you
EDIT
Even the date is set correct in javascript, date in mongo db is seen as 2015-11-16 22:00:00.000Z
To set the date to the start of today, use the setHours() method of the Date object as follows:
var create_date = new Date();
create_date.setHours(0,0,0,0);
console.log(create_date); // prints Tue Nov 17 2015 00:00:00 GMT+0000 (GMT Standard Time)
For UTC, use setUTCHours():
create_date.setUTCHours(0,0,0,0);
The date format i receive from JSON is like this :-
/Date(1412101800000)/
When i convert this into dateformat, i get 1 day minus.
I am using following code :-
var dateFormat = new Date(parseInt(obj['DATEOFJOINING'].substr(6))).toISOString().substr(0, 10);
dateFormat results in 2014-09-30
The Original Date which comes from Db is 2014-10-01
Why is this happening? How to get perfect date?
It's a timestamp.
new Date(parseInt('/Date(1412101800000)/'.substr(6)));
Just set a right timezone.
var dateFormat = new Date(parseInt('/Date(1412101800000)/'.substr(6)));
dateFormat.setTime( dateFormat.getTime() + dateFormat.getTimezoneOffset()*60*1000 );
Date in your timezone*: 30/09/2014 19:30:00
Date in Los Angeles*: 30/09/2014 11:30:00
Date in Berlin* :30/09/2014 19:30:00
Date in Beijing*: 01/10/2014 01:30:00
Date in New York* :30/09/2014 13:30:00
For example
var dateFormat = new Date(parseInt('/Date(1412101800000)/'.substr(6)));
dateFormat.setTime( dateFormat.getTime() + dateFormat.getTimezoneOffset()*(-10*100000));
Date {Wed Oct 01 2014 12:10:00 GMT+0100 (BST)}
As #madforstrength commented, it uses the offset from GMT.
To get the offset in minutes you can try:
var d = new Date()
var n = d.getTimezoneOffset();
and adjust for your local time.
You can see a reference here.
This is due to the clients timezone. Use Date.prototype.getTimezoneOffset to get the offset:
var date = new Date(1412101800000);
var gmtDate = new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000);
I have the upload date for a course saved in a ViewModel variable #Model.Course.UploadDate when calling the following code:
alert('#Model.Course.UploadDate');
I get an output as expected of:
21/01/2014 16:16:13
I know want to check that the uploadDate is within the last 10 seconds before sending a statement to the database but trying to use the following code:
var uploadDate = new Date('#Model.Course.UploadDate.ToLongDateString()');
alert("UPLOAD DATE " + uploadDate);
I get an unexpected output of:
Tue Jan 21 2013 00:00:00 GMT+0000
This is the format that I need the date in only with the saved time data shown. I am then looking to perform a calculation as follows:
var TENSECONDS = 10 * 1000;
var uploadDate = new Date('#Model.Course.UploadDate.ToLongDateString()');
var today = new Date();
var check = today - uploadDate;
if (parseInt(check) > parseInt(TENSECONDS))
alert("ROUTE1");
else
alert("ROUTE2");
Quote from the documentation of the Date object constructor:
value: Integer value representing the number of milliseconds since 1
January 1970 00:00:00 UTC (Unix Epoch).
So actually that's the safest thing to pass to the constructor of a Date object instead of some strings which might be incorrectly interpreted and are completely culture dependent.
So just convert your DateTime instance to the number of milliseconds that elapsed since 1 January 1970 and feed this timestamp to the constructor:
var timestamp = #(Model.Course.UploadDate - new DateTime(1970, 1, 1)).TotalSeconds;
var uploadDate = new Date(timestamp);
As an alternative you could use the ISO8601 format if you intend to be passing a string:
dateString: String value representing a date. The string should be in
a format recognized by the Date.parse() method (IETF-compliant RFC
2822 timestamps and also a version of ISO8601).
So:
var uploadDate = new Date('#Model.Course.UploadDate.ToString("o")');
I solved this using the following code:
var dateArray = new Array();
dateArray = '#Model.Course.UploadDate'.split("/");
var dateD = dateArray[0];
var dateM = dateArray[1];
var dateY = dateArray[2];
var dateT = dateArray[3];
timeArray = dateT.split(":");
var timeH = timeArray[0];
var timeM = timeArray[1];
var timeS = timeArray[2];
var dateUS = dateM + "/" + dateD + "/" + dateY + dateT;
var uploadDate = new Date(dateD,dateM,dateY,timeH,timeM,timeS);
How can I convert a string representation of a date to a real javascript date object?
the date has the following format
E MMM dd HH:mm:ss Z yyyy
e.g.
Sat Jun 30 00:00:00 CEST 2012
Thanks in advance
EDIT:
My working solution is based on the accepted answer. To get it work in IE8, you have to replace the month part (e.g. Jun) with the months number (e.g. 5 for June, because January is 0)
Your date string can mostly be parsed as is but CEST isn't a valid time zone in ISO 8601, so you'll have to manually replace it with +0200.
A simple solution thus might be :
var str = "Sat Jun 30 00:00:00 CEST 2012";
str = str.replace(/CEST/, '+0200');
var date = new Date(str);
If you want to support other time zones defined by their names, you'll have to find their possible values and the relevant offset. You can register them in a map :
var replacements = {
"ACDT": "+1030",
"CEST": "+0200",
...
};
for (var key in replacements) str = str.replace(key, replacements[key]);
var date = new Date(str);
This might be a good list of time zone abbreviation.
You can use following code to convert string into datetime:
var sDate = "01/09/2013 01:10:59";
var dateArray = sDate.split('/');
var day = dateArray[1];
// Attention! JavaScript consider months in the range 0 - 11
var month = dateArray[0] - 1;
var year = dateArray[2].split(' ')[0];
var hour = (dateArray[2].split(' ')[1]).split(':')[0];
var minute = (dateArray[2].split(' ')[1]).split(':')[1];
var objDt = new Date(year, month, day, hour, minute);
alert(objDt);