How to resolve the algorithm Date manipulation step by step in the JavaScript programming language
How to resolve the algorithm Date manipulation step by step in the JavaScript programming language
Table of Contents
Problem Statement
Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format. As extra credit, display the resulting time in a time zone different from your own.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Date manipulation step by step in the JavaScript programming language
The code snippet you provided is a JavaScript function called add12hours
that takes a date string as an argument and returns a new date object that represents the same moment in time as the original date string, but with 12 hours added.
The function first splits the date string into its component parts: the date, month, year, time, and time zone. It then converts the month name to a number, zero indexed. If the month name is invalid, the function returns undefined.
Next, the function adds 12 hours to the hour component of the time. If the ampm
is pm
, the function adds another 12 hours to represent 24-hour time.
The function then creates a new Date object in the local time zone. It sets the hours, minutes, seconds, and milliseconds of the new Date object to the values that were calculated earlier.
Finally, the function adjusts the minutes of the new Date object for the time zone so that it represents the same moment in time as the original date string plus 12 hours.
Here is an example of how to use the add12hours
function:
var inputDateString = 'March 7 2009 7:30pm EST';
console.log(
'Input: ' + inputDateString + '\n' +
'+12hrs in local time: ' + add12hours(inputDateString)
);
The output of this code is:
Input: March 7 2009 7:30pm EST
+12hrs in local time: Sun Mar 08 2009 07:30:00 GMT-0400 (Eastern Standard Time)
Source code in the javascript programming language
function add12hours(dateString) {
// Get the parts of the date string
var parts = dateString.split(/\s+/),
date = parts[1],
month = parts[0],
year = parts[2],
time = parts[3];
var hr = Number(time.split(':')[0]),
min = Number(time.split(':')[1].replace(/\D/g,'')),
ampm = time && time.match(/[a-z]+$/i)[0],
zone = parts[4].toUpperCase();
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
var zones = {'EST': 300, 'AEST': -600}; // Minutes to add to zone time to get UTC
// Convert month name to number, zero indexed. Return if invalid month
month = months.indexOf(month);
if (month === -1) { return; }
// Add 12 hours as specified. Add another 12 if pm for 24hr time
hr += (ampm.toLowerCase() === 'pm') ? 24 : 12
// Create a date object in local zone
var localTime = new Date(year, month, date);
localTime.setHours(hr, min, 0, 0);
// Adjust localTime minutes for the time zones so it is now a local date
// representing the same moment as the source date plus 12 hours
localTime.setMinutes(localTime.getMinutes() + zones[zone] - localTime.getTimezoneOffset() );
return localTime;
}
var inputDateString = 'March 7 2009 7:30pm EST';
console.log(
'Input: ' + inputDateString + '\n' +
'+12hrs in local time: ' + add12hours(inputDateString)
);
You may also check:How to resolve the algorithm Recaman's sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Totient function step by step in the Forth programming language
You may also check:How to resolve the algorithm File input/output step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Find limit of recursion step by step in the UNIX Shell programming language