time-format

JavaScript seconds to time string with format hh:mm:ss

≡放荡痞女 提交于 2019-11-26 03:14:54
问题 I want to convert a duration of time, i.e., number of seconds to colon-separated time string (hh:mm:ss) I found some useful answers here but they all talk about converting to x hours and x minutes format. So is there a tiny snippet that does this in jQuery or just raw JavaScript? 回答1: String.prototype.toHHMMSS = function () { var sec_num = parseInt(this, 10); // don't forget the second param var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var

Format JavaScript date as yyyy-mm-dd

和自甴很熟 提交于 2019-11-26 01:24:32
问题 I have a date with the format Sun May 11,2014 . How can I convert it to 2014-05-11 using JavaScript? function taskDate(dateMilli) { var d = (new Date(dateMilli) + \'\').split(\' \'); d[2] = d[2] + \',\'; return [d[0], d[1], d[2], d[3]].join(\' \'); } var datemilli = Date.parse(\'Sun May 11,2014\'); taskdate(datemilli); The code above gives me the same date format, sun may 11,2014 . How can I fix this? 回答1: You can do: function formatDate(date) { var d = new Date(date), month = '' + (d

How to convert milliseconds to “hh:mm:ss” format?

最后都变了- 提交于 2019-11-26 00:41:08
问题 I\'m confused. After stumbling upon this thread, I tried to figure out how to format a countdown timer that had the format hh:mm:ss . Here\'s my attempt - //hh:mm:ss String.format(\"%02d:%02d:%02d\", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); So, when I try a value like 3600000ms

Convert seconds to HH-MM-SS with JavaScript?

[亡魂溺海] 提交于 2019-11-26 00:19:08
问题 How can I convert seconds to an HH-MM-SS string using JavaScript? 回答1: Don't you know datejs? it is a must know. Using datejs, just write something like: (new Date).clearTime() .addSeconds(15457) .toString('H:mm:ss'); --update Nowadays date.js is outdated and not maintained, so use "Moment.js", which is much better as pointed out by T.J. Crowder. 回答2: You can manage to do this without any external JavaScript library with the help of JavaScript Date method like following: var date = new Date

Get month name from Date

纵饮孤独 提交于 2019-11-25 22:26:59
问题 How can I generate the name of the month (e.g: Oct/October) from this date object in JavaScript? var objDate = new Date(\"10/11/2009\"); 回答1: Shorter version: const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; const d = new Date(); document.write("The current month is " + monthNames[d.getMonth()]); Note (2019-03-08) - This answer by me which I originally wrote in 2009 is outdated. See David Storey's

Javascript add leading zeroes to date

纵然是瞬间 提交于 2019-11-25 22:06:25
问题 I\'ve created this script to calculate the date for 10 days in advance in the format of dd/mm/yyyy: var MyDate = new Date(); var MyDateString = new Date(); MyDate.setDate(MyDate.getDate()+10); MyDateString = MyDate.getDate() + \'/\' + (MyDate.getMonth()+1) + \'/\' + MyDate.getFullYear(); I need to have the date appear with leading zeroes on the day and month component by way of adding these rules to the script. I can\'t seem to get it to work. if (MyDate.getMonth < 10)getMonth = \'0\' +

Convert a Unix timestamp to time in JavaScript

时光毁灭记忆、已成空白 提交于 2019-11-25 21:41:04
问题 I am storing time in a MySQL database as a Unix timestamp and that gets sent to some JavaScript code. How would I get just the time out of it? For example, in HH/MM/SS format. 回答1: // Create a new JavaScript Date object based on the timestamp // multiplied by 1000 so that the argument is in milliseconds, not seconds. var date = new Date(unix_timestamp*1000); // Hours part from the timestamp var hours = date.getHours(); // Minutes part from the timestamp var minutes = "0" + date.getMinutes();

Where can I find documentation on formatting a date in JavaScript? [closed]

筅森魡賤 提交于 2019-11-25 21:32:18
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I noticed that JavaScript\'s new Date() function is very smart in accepting dates in several formats. Xmas95 = new Date(\"25 Dec, 1995 23:15:00\") Xmas95 = new Date(\"2009 06 12,12:52:39\") Xmas95 = new Date(\"20 09 2006,12:52:39\") I could not find documentation anywhere showing all the valid string formats while