Convert EDT timestamp to PST with regex / JavaScript -
a third party providing me edt time-stamp in following format:
mm/dd/yyyy hh:mm
for instance: '08/19/2013 11:31'
i need convert pst javascript (same date time format) , have been looking on can't find info doing this.. if can me example code appreciate it.
if wanted manually, can try following:
- split space, have date , time.
- split time ":" , split date "/".
- create
new date()
, provide right values in right order. - subtract 3 hours using proper methods, recreate format.
here's example of this:
var est = "01/01/2014 02:31", finaldate, pst; finaldate = parsedatestring(est); finaldate.sethours(finaldate.gethours() - 3); pst = formatdate(finaldate); console.log(pst); function parsedatestring(str) { var datetime, date, time, datesplit, month, day, year, timesplit, hour, minute; datetime = est.split(" "); date = datetime[0]; time = datetime[1]; datesplit = date.split("/"); month = datesplit[0] - 1; day = datesplit[1]; year = datesplit[2]; timesplit = time.split(":"); hour = timesplit[0]; minute = timesplit[1]; return new date(year, month, day, hour, minute); } function formatdate(d) { return padzero(d.getmonth() + 1) + "/" + padzero(d.getdate()) + "/" + d.getfullyear() + " " + padzero(d.gethours()) + ":" + padzero(d.getminutes()); } function padzero(num) { if (+num < 10) { num = "0" + num; } return "" + num; }
demo: http://jsfiddle.net/mmvmr/
the padzero
function there prepend 0
s in case number less 10.
reference:
Comments
Post a Comment