J019 Posted June 29, 2019 Share Posted June 29, 2019 I want to use this code, but when establishing a date, I noticed that it is set to GMT+1 and mine is GMT-5. I understand that it is not something serious, but it would be easier to use it in my time zone. Code: https://www.w3schools.com/howto/howto_js_countdown.asp Thanks and regards. Link to comment Share on other sites More sharing options...
JMRKER Posted September 3, 2019 Share Posted September 3, 2019 This is not a count-down example, but it does illustrate the use of Date.toUTCDString() and Date.toString() functions. You should be able to modify for your needs along with the set Date() functions, if necessary. <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/><title> .toUTCString / .toString </title><!-- link rel="stylesheet" href="common.css" media="screen" --><style></style></head><body><h1> Displays in console.log </h1><pre id="demo"></pre><script>const doc = (IDS) => document.getElementById(IDS);/* send console.log to element in body via assignment */// following modified from: https://stackoverflow.com/questions/16616722/sending-all-javascript-console-output-into-a-dom-elementvar former = console.log; // retain former actions of console.logconsole.log = function(...msg){ former(...msg); // maintains existing logging via the console. (optional) doc("demo").append(msg.join(' ')+'\n'); // output needs to be specified}</script><script> console.clear(); const Months = ['January','February','March', 'April', 'May', 'June', 'July', 'August', 'September','October','November','December']; const Days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; Array.prototype.lookFor = function (str) { // returns index of found string or -1 if not found let arr = this; for (let i=0; i<arr.length; i++) { if (arr[i].indexOf(str) != -1) { return i; } } return -1; } var getDaysInMonth = function(month,year) { // Day 0 is the last day in the previous month // return new Date(year, month, 0).getDate(); // Here January is 1 based// Alternative: return Number(new Date(year, month+1, 0).getDate()); // Here January is 0 based }; var getDayOfYear = function(year,month,day) { // Here January is 0 based; var tot = 0; for (let m = 0; m<month; m++) { tot += getDaysInMonth(m,year); } return tot+Number(day); }; Date.prototype.dayDiff = function(d) { return Math.floor(Math.abs(this-d)/86400000); }; console.clear; console.log('new Date.toUTCDString()\t',new Date().toUTCString()); var [day,date,mo,year,time,zone] = new Date().toUTCString().split(' '); console.log('Days-In-Month\t: ',getDaysInMonth(Months.lookFor(mo),year)); console.log('Day-Of-Year\t: ',getDayOfYear(year,Months.lookFor(mo),date)); console.log('Long month\t: ',Months[Months.lookFor(mo)]); console.log('Long day\t: ',Days[Days.lookFor(day.substr(0,3))]); console.log(); console.log('\nnew Date().toString(): \t',new Date().toString()); var [day,mo,date,year,time,zone,...rest] = new Date().toString().split(' '); console.log('Days-In-Month\t: ',getDaysInMonth(Months.lookFor(mo),year)); console.log('Day-Of-Year\t: ',getDayOfYear(year,Months.lookFor(mo),date)); console.log('Long month\t: ',Months[Months.lookFor(mo)]); console.log('Long day\t: ',Days[Days.lookFor(day)]); console.log(); console.log("Butch's BDay"); console.log('new Date().toString(1947,7,16): ',new Date(1947,7,16).toString()); var [day,mo,date,year,time,zone,...rest] = new Date(1947,7,16).toString().split(' '); console.log('Days-In-Month\t: ',getDaysInMonth(Months.lookFor(mo),year)); console.log('Day-Of-Year\t: ',getDayOfYear(year,Months.lookFor(mo),date)); console.log('Long month\t: ',Months[Months.lookFor(mo)]); console.log('Long day\t: ',Days[Days.lookFor(day)]); var today = new Date(), Jbday = new Date(1947,7,16), daysbetween = today.dayDiff(Jbday); console.log('days between\t: ', daysbetween, 'days or', (daysbetween/365).toFixed(2), 'years'); console.log(); console.log("Spike's BDay"); console.log('new Date().toString((1951,6,30): ',new Date(1951,6,30).toString()); var [day,mo,date,year,time,zone,...rest] = new Date(1951,6,30).toString().split(' '); console.log('Days-In-Month\t: ',getDaysInMonth(Months.lookFor(mo),year)); console.log('Day-Of-Year\t: ',getDayOfYear(year,Months.lookFor(mo),date)); console.log('Long month\t: ',Months[Months.lookFor(mo)]); console.log('Long day\t: ',Days[Days.lookFor(day)]); var today = new Date(), Kbday = new Date(1951,6,30), daysbetween = today.dayDiff(Kbday); console.log('days between\t: ', daysbetween, 'days or', (daysbetween/365).toFixed(2), 'years');</script></body></html> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now