Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="content">

  <h3>Todays Current Date</h3>
  <p id="today-now"></p>
  <h3>Todays Start and End Times </h3>
  <p id="today-start"></p>
  <p id="today-end"></p>

  <h3>More usable output</h3>
  <p id="today-date"></p>
  
  <p id="today-date-time"></p>
    <hr> 
   <h3>Months (Date Range)</h3>
  <h5>This Month</h5>
  <p id="this-month">
    
  </p>
  <h5>Last three months</h5>
  <p id="last-three-months"></p>
  
  <h5>Last twelve months</h5>
  <p id="last-twelve-months"></p>
  <hr> 
  
  <p>Working with javascript dates can be tricky there are additional libraries you can get and inject into your projects, but I like to keep things dependency free as far as possible.</p>

  <p>The date returned will be your <strong>current</strong> local time, this does not help when your API requires time for reporting. eg: all sales between now and now will be empty, as there would in most cases be no transactions happening on this exact millisecond.</p>

  <p>You need convert it to start at the start of the day to the end of the day, I simply set the hours to 24 hours on the date and then subtract one millisecond.</p>
  
  <h3>Update: 2022/02/03</h3>
  <p>Required a more friendly way to convert dates and either get the time, date or date time. So I added a function that allows for this now.</p>
  <code>friendlyDate(date,includeTime, includeDate)</code>
</div>
              
            
!

CSS

              
                body{
  background: #131417;
  margin: 0 auto;
  font-family: SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace;
}
.content{
  padding: 2em;
  color: #fff;
  width: 800px;
  max-width:100%;
}
              
            
!

JS

              
                let now = new Date();
let startDate: number;
let endDate: number;
let rangeDates = [];

startDate = new Date(now.setHours(0, 0, 0, 0));

endDate = new Date(now.setHours(24, 0, 0, 0) - 1);

//Output to HTML
// Todays Date
document.getElementById("today-now").innerHTML = now;

document.getElementById("today-start").innerHTML = startDate;

document.getElementById("today-end").innerHTML = endDate;

document.getElementById("today-date").innerHTML =
  friendlyDate(startDate, false, true) + " (yy/mm/dd)";

now = new Date();
document.getElementById("today-date-time").innerHTML =
  friendlyDate(now, true, true) + " (h:m:s) asd";

function friendlyDate(date, includeTime = true, includeDate = false) {
  const d = new Date(date);
  let dateString = "";

  if (includeDate)
    dateString +=
      d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();

  if (includeTime)
    dateString += d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();

  return dateString;
};



// pass through the number of the month January = 0 December = 11
// Defaults to current month
// you can also just pass the amount of months you would like to go back eg -3 will take you back three months
function startToEndMonths(
  startMonth = now.getMonth(), 
  endMonth = now.getMonth()
){
  if(startMonth < 0) startMonth = now.getMonth() + startMonth;
  const startDate = new Date(now.getFullYear(), startMonth, 1);
  const endDate = new Date(now.getFullYear(), endMonth, 0);
  return startDate + " to " + endDate;
};

document.getElementById("this-month").innerHTML = startToEndMonths();

document.getElementById("last-three-months").innerHTML = startToEndMonths(-3);

document.getElementById("last-twelve-months").innerHTML = startToEndMonths(-12);
              
            
!
999px

Console