I want to get the previous month total day count
Code
Dim period as string
period = '01/2011'
totdays = DateDiff("d", txtPeriod, DateAdd("m", 1, txtPeriod))
'this will give the totaldays of the month...
But i want to get total days of the previous month
User will type current month only, but code should validate previous month
Expected Output
If period = '02/2011' means then it should display 31 days 'January
If period = '03/2011' means then it should display 28 days 'February
How to do this...
Any Help
This worked fine for me. Also, why do you have a variable period, but use txtPeriod in your calculations?
Dim dt As Date
Dim DaysInMonth As Integer
dt = CDate(txtPeriod.Text)
dt = DateAdd("m", -1, dt)
DaysInMonth = DateDiff("d", dt, DateAdd("m", 1, dt))
Related
I have the following query that gets the week of a date:
SELECT pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww') semana,
SUM (rta.kms_acumulados) kms,
COUNT
(DISTINCT (CASE
WHEN v.secuencia BETWEEN rta.sec_origen AND rta.sec_destino
THEN v.cod_inc
ELSE '0'
END
)
)
- 1 numincidencias
FROM (SELECT ms.tren, ms.fecha_origen_tren, ms.secuencia, ri.cod_inc
FROM r_incidencias ri, mer_sitra ms
WHERE ri.cod_serv = ms.tren
AND ri.fecha_origen_tren = ms.fecha_origen_tren
AND ri.cod_tipoin IN (SELECT cod_tipo_iincidencia
FROM v_tipos_incidencias
WHERE grupo = '45')
AND ri.punto_desde = ms.cod_estacion) v,
r_trenes_asignar rta,
r_maquinas rm,
planificador.pl_dh_material pdm
WHERE rta.fecha BETWEEN TO_DATE ('21/09/2018', 'dd/mm/yyyy') AND TO_DATE ('21/09/2018',
'dd/mm/yyyy'
)
AND rta.serie >= 4000
AND rta.matricula_ant IS NOT NULL
AND rm.matricula_maq = rta.matricula_ant
AND rm.cod_serie = pdm.id_material
AND rta.grafico BETWEEN pdm.desde AND pdm.hasta
AND v.tren(+) = rta.tren
AND v.fecha_origen_tren(+) = rta.fecha
GROUP BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
ORDER BY pdm.serie, rta.matricula_ant, TO_CHAR (fecha, 'ww')
For example week 1
I want to display
week 1 : 1 january - 7 january
How can I get this?
Oracle offers the TRUNC(datestamp, format) function to manipulate dates this way. You may use a variety of format strings to get the first day of a quarter, year, or even the top of the hour.
Given a particular datestamp value, Oracle returns midnight on the first day of the present week with this expression:
TRUNC(datestamp,'DY')
You can add days to a datestamp. Therefore this expression gives you midnight on the last day of the week
TRUNC(datestamp,'DY') + 6
A WHERE-clause selector for all rows in the present week might be this.
WHERE datestamp >= TRUNC(SYSDATE,'DY')
AND datestamp < TRUNC(SYSDATE,'DY') + 7
Notice that the end of the range is just before (<) midnight on the first day of the next week. You need that because you may have datestamps after midnight on the last day of the week. (Beware using BETWEEN for datestamp ranges.)
And,
SELECT TO_CHAR(TRUNC(SYSDATE,'DY'),'YYYY-MM-DD'),
TO_CHAR(TRUNC(SYSDATE,'DY')+6,'YYYY-MM-DD')
FROM DUAL;
displays the first and last dates of the present week in ISO-like format.
Date arithmetic is cool. It's worth your trouble to study the date-arithmetic functions in your DBMS at least once a year.
I'm new to vb scripts please help me.
I'm generating Random year by appending last two digits randomly as shown bellow but i want to generate random date DDMMYYYY format between start date and end date provided by user.
Randomize
tmp = Int((92 - 70 + 1) * Rnd + 70)
tmp1= "01/05/19" & tmp
Use this as a starting point
Option Explicit
Function getRandomDate( startDate, endDate )
getRandomDate = DateAdd( _
"d" _
, Fix( DateDiff("d", startDate, endDate ) * Rnd ) _
, startDate _
)
End Function
Dim startDate, endDate
startDate = CDate("2016/03/10")
endDate = CDate("2017/09/30")
Randomize
Dim i
For i = 0 To 100
WScript.Echo getRandomDate( startDate, endDate )
Next
It just calculates the number of days between the start and end dates and selects a random in this range of days to add to the start date.
As an addendum to #mc-nd's answer
When using Rnd() to generate random numbers you sure to use Randomize to initialise the Random Number Generator using the System Time as a seed value or this happens.
Option Explicit
Function getRandomDate( startDate, endDate )
getRandomDate = DateAdd( _
"d" _
, Fix( DateDiff("d", startDate, endDate ) * Rnd ) _
, startDate _
)
End Function
Dim startDate, endDate
startDate = CDate("2016/03/10")
endDate = CDate("2017/09/30")
Dim i
For i = 0 To 10
WScript.Echo getRandomDate( startDate, endDate )
Next
1st Output:
15/04/2017
07/01/2017
02/02/2017
21/08/2016
28/08/2016
24/05/2017
17/03/2016
16/05/2017
16/06/2017
17/04/2017
04/04/2016
2nd Output:
15/04/2017
07/01/2017
02/02/2017
21/08/2016
28/08/2016
24/05/2017
17/03/2016
16/05/2017
16/06/2017
17/04/2017
04/04/2016
Add the Randomize statement to the code to see the difference;
Option Explicit
Function getRandomDate( startDate, endDate )
getRandomDate = DateAdd( _
"d" _
, Fix( DateDiff("d", startDate, endDate ) * Rnd ) _
, startDate _
)
End Function
Dim startDate, endDate
startDate = CDate("2016/03/10")
endDate = CDate("2017/09/30")
Dim i
'Call the random number generator with the system time as the seed.
Call Randomize()
For i = 0 To 10
WScript.Echo getRandomDate( startDate, endDate )
Next
1st Output:
14/11/2016
28/09/2016
26/05/2016
06/01/2017
22/09/2016
13/06/2016
12/11/2016
05/03/2017
05/05/2016
01/05/2016
03/02/2017
2nd Output:
08/03/2017
20/01/2017
18/04/2016
29/11/2016
16/08/2016
07/05/2016
06/10/2016
27/01/2017
22/07/2016
19/07/2016
21/09/2017
It's an easy thing to miss but makes a BIG difference.
Dim Date1 As Date, Date2 As Date
Date1 = 10/02/2017
Date2 = 10/06/2017
iDiff = DateDiff("d", Date1, Date2, vbMonday)
Dim RndDate As Date
Randomize
RndDate = DateAdd("d", Int((iDiff * Rnd) + 1), Date1)
I find simple way to generate random Date.
How can I output a date minus 3 hours? Example:
BookDate = 8/4/2014 9:00:00 PM
DesirebleDate = 8/4/2014 6:00:00 PM
Tried DesirebleDate = DateDiff("h", BookDate , -3) but it is outputing the amount of hours. Thanks.
DesirableDate = DateAdd("h", -3, BookDate)
Should set DesirableDate to 3 hours earlier than BookDate. DateDiff gives the difference between two dates, which is why you are getting the 3...
I want to add months with last day in current date by using
=dateadd(dateinterval.month, +4, DateAdd("d",-(Day(today)), Today))
expression.
output is
current_date = 12/02/2014
finish_date = 03/30/2014
The problem is that finsih_date month is 03(March) and last day of March is 31 but my parameter showing 30.
It may look like:
=DateAdd("d", -1, DateSerial(DatePart("yyyy", finish_date), DatePart("m", DateAdd("m", 1, finish_date)), 1))
For every day in a given month it calculates the last day of the given month. So for 03/30/2014 you would get 03/31/2014.
Calculation works like: Build new date which is set to the first day of the following month of a given date. Substract 1 day. Which is the last day of the month before. As we added 1 month it is the last day of the month of the given date.
Edit
Code with finished_date = TODAY + 4 Month (finished_date + 1 so it is TODAY + 5)
=DateAdd("d", -1, DateSerial(DatePart("yyyy", DateAdd("m", 5, TODAY())), DatePart("m", DateAdd("m", 5, TODAY())), 1))
how to generate date along with time randomly between a start date and end date using VB script. We have to take input from user
Example: startDate:15/09/2013 9:00:00 Enddate 21/09/2013 15:00:00
Output : Random date :17/09/2013 12:00:00
Add a random number of seconds less than the difference between the dates in seconds to the startDate:
Dim StartDate : StartDate = #15/09/2013 9:00:00#
Dim Enddate : Enddate = #21/09/2013 15:00:01#
Dim SecDiff : SecDiff = DateDiff("s", StartDate, Enddate)
WScript.Echo StartDate, Enddate, SecDiff
Dim n
For n = 1 To 20
WScript.Echo DateAdd("s", Fix(SecDiff * Rnd()), StartDate)
Next
output (german locale):
15.09.2013 09:00:00 21.09.2013 15:00:01 540001
19.09.2013 18:49:56
18.09.2013 17:00:49
18.09.2013 23:55:40
17.09.2013 04:26:04
17.09.2013 06:17:32
20.09.2013 05:12:40
...