DatePart

From NSB App Studio
Jump to: navigation, search

DatePart(interval, date[, firstdayofweek[, firstweekofyear]])

Description

DatePart returns a number specifying a part of the given date. The required parameter, interval, determines the part of date which is measured and returned. The optional parameter firstdayofweek is Sunday, if not specified The optional firstweekofyear is the week containing January 1, if not specified.

Interval Values

Value Description
yyyy Year
q Quarter
m Month
y Day of year
d Day
w Weekday
ww Week of year
h Hour
n Minute
s Second

Example (Basic)

Rem DatePart Example
'DatePart returns a number from part of a date
Dim QuarterPart, MonthPart, DayPart
QuarterPart = DatePart("q", Now)
MonthPart = DatePart("m", Now)
DayPart = DatePart("d", Now)
Print "Today is day " & DayPart
Print "of month " & MonthPart
Print "in quarter " & QuarterPart

Example (JavaScript)

// DatePart Example
/* DatePart returns a number from part of a date */

Date.prototype.DatePart = function(typ) {
  dt=this;
  if (!typ) return dt;
  switch (typ.toLowerCase()) {
    case 'yyyy':
      return dt.getFullYear(); break;
    case 'q':
      return Math.ceil((dt.getMonth()+1)/3); break;
    case 'm':
      return dt.getMonth()+1; break;
    case 'y':
      var dt1 = new Date('1/1/'+dt.getFullYear());
      nDays = (dt-dt1) / 86400000;
      nDaysDiff = Math.round(nDays+1);
      return nDaysDiff; break;
    case 'd':
      return dt.getDate(); break;
    case 'w':
      return dt.getDay()+1; break;
    case 'ww':
      var dt1 = new Date('1/1/'+dt.getFullYear());
      nDays = (Math.floor((dt-dt1)/86400000)+(dt1.getDay()+1));
      nWeek = Math.ceil((nDays)/7);
      return nWeek; break;
    case 'h':
      return dt.getHours(); break;
    case 'n':
      return dt.getMinutes(); break;
    case 's':
      return dt.getSeconds(); break;
  }
  return dt;
}

var QuarterPart, MonthPart, DayPart;
var dt = new Date();
QuarterPart = dt.DatePart("q");
MonthPart = dt.DatePart("m");
DayPart = dt.DatePart("d");
document.write("Today is day " + DayPart + "<br>");
document.write("of month " + MonthPart + "<br>");
document.write("in quarter " + QuarterPart + "<br>");

Output

Today is day 18
of month 8
in quarter 3
(''sample output from August 18, 1998'')

Related Items

DateAdd, DateDiff