Hey, just wondering how to convert an HH:MM string into a javascript Date object. I have tried new Date(string); and myDate.setTime() but to no avail.
A side question could be: How to convert a string in HH:MM into milliseconds from Jan 1, 1970.
Thanks for your help in advance.
How about something like:
//using timestr '10:33:21', could also be '10-33-21'
var dat = new Date, time = timestr.split(/\:|\-/g);
dat.setHours(time[0]);
dat.setMinutes(time[1]);
in JavaScript, I'm using the datejs library. http://www.datejs.com/
If you include this library, you have a function called "parseExact" and you could use it like this:
var dateString = "10-12";
var date = new Date.parseExact(dateString, "hh-mm");
To get the miliseconds, you can download the file time.js from http://code.google.com/p/datejs/source/browse/trunk/#trunk/src. Then you have a function getTotalMilliseconds() you can use:
var mSeconds = date.getTotalMilliseconds();
I hope this will help a little bit.
Related
I've got a column of datetimes in the following funky format:
2016-07-07 12:34:47 -0700
I understand that I can convert this to a usable format by using the .toISOString() function in JS/Apps Script.
Not to be presumptuous, but can anybody easily manage this feat, help me take the first step, or even point me in the direction perhaps of a published Apps Script that can do this?
Thanks so much :)
Return the Date object as a string, using locale conventions:
var d = new Date();
var n = d.toLocaleDateString();
Convert today's date into a readable string:
var d = new Date();
var n = d.toDateString();
Other functions that might be useful:
.toUTCString()
.toString()
I have a time stamp of the in following form
2013-05-05 16:00:00
Can anyone tell me how to get rid of the time part and advance it one day using javascript, so I have.
2013-05-06
var date1 = new Date("2013-05-06 16:00:00");
var date2 = new Date(date1.getFullYear(),date1.getMonth(),date1.getDate()+1)
Ok well make the time stamp a string and use the .split() method on the space that separates the date and time take a look here for more information: http://www.w3schools.com/jsref/jsref_split.asp
Or more appropriately use the requisite date function like new Date(year,month,day); http://www.w3schools.com/jsref/jsref_obj_date.asp
I have a system that returns a JSON object that contains dates in string format.
These dates are in the format "2012-10-19 06:05:38 GMT" (no... I'm stuck with them like this)
So I need to get this into a date object (d) ready to output as d.toLocaleDateString()
In chrome it works perfectly by just passing the string to a new Date (Bad bad Chrome - makes Eric lazy), but of course it fails in FF and IE
I can fix it by splitting the string but its not pretty and I've not figured out dealing with the offsets from GMT.
There must be a more elegant way...?
I'm sure someone here can do it in one line.
It's not quite a one-liner, but if you know all your dates will be GMT, something like the following should work:
function parseDate(dateString) {
// [y, m, d, hr, min, sec]
var parts = dateString.match(/\d+/g);
// Months are 0-indexed
parts[1] -= 1;
return new Date(Date.UTC.apply(Date, parts));
}
If I were you, and had access to the serverside script gathering that information (and outputting it) I would convert the date into a unix timestamp, and then make Javascript process that using the Date constructor easily.
EDIT: You can use strtotime() function to convert the string date into numeric unix timestamp if you're using PHP.
If you know the exact format, you could use a library such as Moment.js: Documentation for Moment.js.
To parse:
var dateString = "2012-10-19 06:05:38 GMT".replace(" GMT", "");
var date = moment(dateString, "YYYY-MM-DD HH:mm:ss");
You can just parse the dateString manually,and pass the Date the Date constructor exactly:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
var dateString = "2012-10-19 06:05:38 GMT".split(" "),
date = dateString[0].split("-"),
time = dateString[1].split(":");
var dateObj = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);
I have a date in this string format "02/28/2012" and I want to convert it to UTC.
I'm using the jquery datepicker to select thedate and populate an inputbox. any clues?
Thanks
var datestr = "07/08/2005";
var datearr = datestr.split("/")
var utc = Date.UTC(datearr[2],datearr[0],datearr[1]);
var utcdate = Date.UTC(2012,2,28);
The other answers are good, but they will give you the wrong result.
In Javascript, the month argument is zero-indexed, so make sure to subtract 1 from the standard month number,
var utcms = Date.UTC(2012,2-1,28);
Unfortunately jquery .datepicker.parseDate(str) injects a local timezone (it would be nice if the documentation said this), and Date(str) and Date.parse(str) appear unpredictable about their treatment of local vs UTC.
I am using this piece of code to get string representing date yyyy-mm-dd from a hidden field and then format it as needed:
var date_string = $('#end-date').val();
var splitDate = date_string.split("-");
var end_date = new Date(splitDate[0], splitDate[1] - 1, splitDate[2]);
end_date.format("dddd, mmmm dS, yyyy")
But it throws an error:
end_date.format is not a function
Why does it happen and how to solve this issue?
That is because .format is not a native JavaScript function on Date.prototype.
You need to add a lib like this one: http://jacwright.com/projects/javascript/date_format/
I personally use http://momentjs.com/ to manage dates in JavaScript
You get this error because Date.prototype.format just does not exist (I am wondering why you think it does).
See this question about solutions how to format dates.