In my web app user needs to fill the fields like date and time.
In date they can enter a date in a format:
d/m/yy
or
dd/mm/yy
(unfortunately I can't control how exactly user will enter it)
And they can also pick a time from the drop-down list:
08:00am,8.30am, 09:00am,.... , 05:00 pm
Now, I am trying to convert time and date strings it into a valid date object using function below, but I am getting "Date isInvalid Date" output.
How should I proceed with it?
var timeStartString ="1970/01/01 "+ "10:00am";
var st1 =createDate("1/12/2013",timeStartString);
Logger.log("Date is"+st1);
function createDate(dateString,timeString)
{
var eventDate = new Date(dateString);
var eventTime = new Date(timeString)
message +="Date String:"+dateString + " timeString "+timeString;
eventTime.setFullYear(eventDate.getFullYear());
eventTime.setDate(eventDate.getDate());
eventTime.setMonth(eventDate.getMonth());
return eventTime;
}
You may want to look into using the Moment.js library, which makes date parsing and manipulation in JavaScript a bit easier. More information on how to use it in Apps Script here. If the date format is consistent, then you can use a custom format string to parse it.
var date = moment('1970/01/01 10:00am', 'YYYY/MM/DD HH:mma').toDate();
The Date class won't recognize the time if am or pm is directly attatched to the time. For example new Date("1/12/2013 10:00am") will return Invalid Date, whereas new Date("1/12/2013 10:00 am") will return Sat Jan 12 2013 22:00:00. It's a subtle difference, but that's the way the parser is built. You shouldn't need the createDate method at all if you concatenate the date with the properly formatted time string.
Related
I am trying to read all dates in a table and see if those dates are old dates than current date time and if those are old dates then highlight those dates with some color.
Here is my Javascript code
$(".ticket-gird-td-duedate").each(function(i, e) {
debugger;
var dueDateAsString = $(e).text();
console.log(dueDateAsString);
var dueDate = new Date(dueDateAsString);
var currentdate = new Date();
if (dueDate < currentdate) {
//mark date in red color
console.log("I need to change color for this date as this is past date" + dueDate);
}
});
Problem here is dueDateAsString comes as "07/10/2017 18:30 PM"
And when I am doing
new Date("07/10/2017 18:30 PM")
it fails with invalid date error
Invalid Date
How can I convert my string date to Javascript date and proceed to compare it with current date?
That isn't a valid date, as you either have 24hr format or AM|PM 12hr format.
This works:
new Date('07/10/2017 18:30'); // No 'PM' after the 24hr time
Also note that JS dates are mutable, so todayDate and check will hold the same date value, but check will be a number.
You call this one first
todayDate.setDate(todayDate.getDate() - 5);
It will update todayDate to 5 days ago. Then you just assign it to check or you todayDate further.
var check = todayDate;
Hope this helps.
You can do this by using moment.js library (yet another terrific way to achieve date parsing).
Moment.Js library is freely available. You can use either a minified or full version of this library as you require. In this
library all the operations are performed on a moment object so the
date or time is stored in this library as a moment object only. So
this moment object is the core of this entire library. You can find
this library at Moment.js site's home page.
Add momentjs library reference into your project - https://momentjs.com/downloads/moment.js
updated code with moment.js would be:
var todayDate = new moment().format("MM/DD/YYYY h:mm:ss a");
var yesterday = todayDate.toLocaleString();
var check = moment(todayDate, "MM/DD/YYYY h:mm:ss a").add('days', -5);
alert("Your Old Date is- " + todayDate);
alert(check);
alert("Your Old Date is- " + yesterday);
JavaScript fiddle
In my mean stack app, i have data on a date basis. In angular side, i used a date picker to get/set the date that the read/write of Data to be handled. The date picker produce date of the form "dd-mm-yyyy". What is the easiest way to convert this into mongodb understandable format, and back.
var str = "29-1-2016";
darr = str.split("-"); // ["29", "1", "2016"]
var dobj = new Date(parseInt(darr[2]),parseInt(darr[1])-1,parseInt(darr[0]));
// Date {Fri Jan 29 2016 00:00:00 GMT+0530(utopia standard time)
console.log(dobj.toISOString());
//2016-01-28T18:30:00.000Z
This will do it, but is there an easier way..!!
Also please comment on why in the isodate format i get 2016-01-28T...., other than 2016-01-29T.....
You could use this solution (worked in my case)-
Firstly, use Moment.js in your code, include it in your project. Now, The time string that you are getting here var str = "29-1-2016"; and along with moment.js use the below code and you're good to go -
var str = "29-1-2016";
var time = moment(str).toISOString();
\\ This variable time is now converted into ISO string
I am getting date from XML but date is in plain string format. I would like to create the difference from today's date and time and the date and time which i am getting from xml.
For example i am getting date as a plain string in this format (2012-10-17T08:15:19.500-05:00).Now when i am doing difference with current date&time than i need to display something like this "2:hr,32min".
Any help/suggestion would be a great input.
Thanks
This should work:
var myDate = new Date( '2012-10-17T08:15:19.500-05:00' ),
newDate = new Date();
Browser results can vary when parsing dates. I have a test Fiddle here.
To get the difference between dates: var diff = myDate - newDate; and to convert that back to something useful: Convert time interval given in seconds into more human readable form
How can I convert a date from:
Thu, 1 July 2011 22:30:00 to '2011-07-01T13:51:50.417' using javascript.
I get the UTC format when I do a new date.
IE causes me issues when I first create a date object as it shows: NaN
You could generate a new Date-Object and then get the different parts:
var today = new Date();
var year = today.getFullYear(); // Returns 2012
var month = today.getMonth()+1; // Returns the month (zero-based)
...
Then you can create a new string like you need it.
possible duplicate try search next time
stackoverflow question
Try http://www.datejs.com/. It is a JavaScript Date Library with an extended Date.parse method and a Date.parseExact method, which lets you specify a format string. See DateJS APIDocumentation.
and then you can manipulate it as you want
The d3.js library has some very solid routines for date conversions. See https://github.com/mbostock/d3/wiki/Time-Formatting#wiki-parse.
I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)