milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. JavaScript
  4. JavaScript

Increasing day and formating date time

Details
Written by: Stanko Milosev
Category: JavaScript
Published: 28 January 2009
Last Updated: 08 July 2013
Hits: 5885

First, code for increasing day. If we have date in string, then code would look something like this:

strYear = strDate.substring(0,4);
strMonth = strDate.substring(5,7);
strDay = strDate.substring(8,10);
d = new Date(strYear, strMonth - 1, strDay);
d.setDate(d.getDate() + 1);

Then, to format date time, to something like yyyy-mm-dd, I was using this code:

strYear = d.getFullYear();
if (d.getMonth() < 10)  {  
	intMonth = d.getMonth() + 1;
	strMonth = '0' + intMonth;
} else  {
  strMonth = d.getMonth() + 1;
}
if (d.getDate() < 10)  {
  strDay = '0' + d.getDate();
} else {  
	strDay = d.getDate();   
}

Finnaly, date would be created something like:

strDate = strYear + '-' + strMonth + '-' + strDay;

getMonth - with this function we are geting number of a month, but from 0, 0 - this is January?
getDate - with this function we are geting number of a day in a month.

Page 9 of 9

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9