happyproff Posted April 9, 2008 Report Share Posted April 9, 2008 Есть необходимость вывести число (минуты) в двухзначном формате, независимо от значения. Проще говоря "01" вместо "1", и "10".var now = new Date();var minutes = now.getMinutes();document.write (minutes); Quote Link to comment Share on other sites More sharing options...
0 ZoNT Posted April 9, 2008 Report Share Posted April 9, 2008 document.write(minutes Quote Link to comment Share on other sites More sharing options...
0 happyproff Posted April 9, 2008 Author Report Share Posted April 9, 2008 а я нашел вот такое решение, криво, грязно, мерзко и противно, но что делать, я на js начал писать несколько часов назад var now = new Date();var minutes = now.getMinutes();if (minutes < 10) { var mins = "0" + minutes;} else { var mins = minutes;}document.write(mins); Quote Link to comment Share on other sites More sharing options...
0 ZoNT Posted April 9, 2008 Report Share Posted April 9, 2008 , так это то же самое, просто я тебе написал в краткой форме. Quote Link to comment Share on other sites More sharing options...
0 happyproff Posted April 9, 2008 Author Report Share Posted April 9, 2008 ZoNT, спасибо, вс? отлично. Единственное но: пришлось каждый такой форматированный элемент выводить отдельным потоком. Так:document.write(hours<10 ? "0"+hours : hours);document.write(":");document.write(minutes<10 ? "0"+minutes : minutes); Quote Link to comment Share on other sites More sharing options...
0 happyproff Posted April 9, 2008 Author Report Share Posted April 9, 2008 , так это то же самое, просто я тебе написал в краткой форме.Так я понимаю, что тоже самое, просто мой вариант очень не красивый на фоне твоего сокращенного. Quote Link to comment Share on other sites More sharing options...
0 ZoNT Posted April 9, 2008 Report Share Posted April 9, 2008 function To2(val) { return (val<10 ? "0"+val : val);}document.write(To2(hours)+':'+To2(minutes)+':'+To2(seconds)); Quote Link to comment Share on other sites More sharing options...
0 happyproff Posted April 9, 2008 Author Report Share Posted April 9, 2008 изящно, сенкс Quote Link to comment Share on other sites More sharing options...
0 AKS Posted April 9, 2008 Report Share Posted April 9, 2008 document.write((new Date).toLocaleTimeString()); Quote Link to comment Share on other sites More sharing options...
0 happyproff Posted April 9, 2008 Author Report Share Posted April 9, 2008 AKS, требуется отображать только часы и минуты. А вообще скрипт показывает:18:469 апреля 2008скрипт уже работает:<SCRIPT LANGUAGE="JavaScript"> var now = new Date(); var hours = now.getHours(); var minutes = now.getMinutes(); mydate = new Date(); mymonth = mydate.getMonth(); weekday= mydate.getDate(); year= mydate.getYear(); var browser=navigator.appName if (browser!="Microsoft Internet Explorer") { year+= 1900; } if(mymonth == 0) month = "января " else if(mymonth ==1) month = "февраля " else if(mymonth ==2) month = "марта " else if(mymonth ==3) month = "апреля " else if(mymonth ==4) month = "мая " else if(mymonth ==5) month = "июня " else if(mymonth ==6) month = "июля " else if(mymonth ==7) month = "августа " else if(mymonth ==8) month = "сентября " else if(mymonth ==9) month = "октября " else if(mymonth ==10) month = "ноября " else if(mymonth ==11) month = "декабря" function To2(val) { return (val<10 ? "0"+val : val); } document.write(To2(hours) + ":" + To2(minutes) + "" + weekday + " " + month + year);s</SCRIPT>Спасибо Quote Link to comment Share on other sites More sharing options...
0 ZoNT Posted April 9, 2008 Report Share Posted April 9, 2008 Метод toLocaleTimeString преобразует примитивное значение объекта дата в строку времени по местному времени с учетом локальных установок операционной системы. Формат результирующей строки зависит от локализации операционной системы и обозревателя Quote Link to comment Share on other sites More sharing options...
0 WingedFox Posted April 9, 2008 Report Share Posted April 9, 2008 happyproffИспользуйте getFullYear и не надо будет детектить браузеры.var month = ["января ", "февраля ", "марта ", "апреля ", "мая ", "июня ", "июля ", "августа ", "сентября ", "октября ", "ноября ", "декабря"][mymonth];выглядит куда изящнее, нежели пачка if. Quote Link to comment Share on other sites More sharing options...
0 AKS Posted April 9, 2008 Report Share Posted April 9, 2008 (function () { var d = new Date; var t = /^d{1,2}[^d]+d{1,2}/.exec(d.toLocaleTimeString()); document.write(t + '' + d.toLocaleDateString()); }()); Quote Link to comment Share on other sites More sharing options...
Question
happyproff
Есть необходимость вывести число (минуты) в двухзначном формате, независимо от значения. Проще говоря "01" вместо "1", и "10".
Link to comment
Share on other sites
12 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.