not able to format results in Dateadd - vbscript

Function CurrMonthName(dateval)
Dim tmp : tmp = DateAdd("m", 0, dateval)
CurrMonthName = MonthName(Month(tmp))
CurrMonthName = replace(CurrMonthName , "%M",CurrMonthName)
i am trying to format the month name so that it displays first 3 letters. On searching found %M can be used. I tried using it in above code and results are not coming as expected. It is still displaying full month

In VBScript, the way to get an abreviated Monthname is MonthName.
>> WScript.Echo MonthName(Month(Date()), True)
>>
Jul
>>
See this answer for a more flexible way to format dates (and other kind of data).

Use the function "Left" to cut the first off: Left("February",3) gives you "Feb"

Related

Substracting one Hour from Date in ISO-8601 format using VBScript [duplicate]

I was wondering if someone could help me.
I'm very new at ASP I want to format the current date and time as follows:
yyyy-mm-dd hh:mm:ss
But all i can do is the following
Response.Write Date
Can someone help me out please.
Date formatting options are limited in Classic ASP by default, there is a function FormatDateTime() which can format your date is various ways based on the servers regional settings.
For more control over date formatting though there are built in date time functions
Year(date) - Returns a whole number representing the year. Passing Date() will give back the current year.
Month(date) - Returns a whole number between 1 and 12, inclusive, representing the month of the year. Passing Date() will return the current month of the year.
MonthName(month[, abbv]) - Returns a string indicating the specified month. Passing in Month(Date()) as the month will give back the current Month string. As suggested by #Martha
Day(date) - Returns a whole number between 1 and 31, inclusive, representing the day of the month. Passing Date() will return the current day of the month.
Hour(time) - Returns a whole number between 0 and 23, inclusive, representing the hour of the day. Passing Time() will return the current hour.
Minute(time) - Returns a whole number between 0 and 59, inclusive, representing the minute of the hour. Passing Time() will return the current minute.
Second(time) - Returns a whole number between 0 and 59, inclusive, representing the second of the minute. Passing Time() will return the current second.
IMPORTANT:
When formatting date / time values, always store the date / time value first. Also, any needed calculations (DateAdd() etc.) should be applied before attempting to format or you will get unexpected results.
The functions Month(), Day(), Hour(), Minute() and Second() all return whole numbers. Luckily there is an easy workaround that lets you pad these values quickly Right("00" & value, 2) what it does is append 00 to the front of the value then from the right take the first two characters. This ensures that all single digit values return prefixed with a 0.
Dim dd, mm, yy, hh, nn, ss
Dim datevalue, timevalue, dtsnow, dtsvalue
'Store DateTimeStamp once.
dtsnow = Now()
'Individual date components
dd = Right("00" & Day(dtsnow), 2)
mm = Right("00" & Month(dtsnow), 2)
yy = Year(dtsnow)
hh = Right("00" & Hour(dtsnow), 2)
nn = Right("00" & Minute(dtsnow), 2)
ss = Right("00" & Second(dtsnow), 2)
'Build the date string in the format yyyy-mm-dd
datevalue = yy & "-" & mm & "-" & dd
'Build the time string in the format hh:mm:ss
timevalue = hh & ":" & nn & ":" & ss
'Concatenate both together to build the timestamp yyyy-mm-dd hh:mm:ss
dtsvalue = datevalue & " " & timevalue
Call Response.Write(dtsvalue)
Note: You can build the date string in one call but decided to break it down into the three variables to make it easier to read.
How Can I Format Date
Example of Parsing a Date String (Answers provide approaches to taking a date string format and parsing it to a valid Date variable).
Format the date of the previous day format yyyymmdd with VBScript (Example of why storing date / time before performing formatting is important)
VBScript ISO8601 (Example of functions to construct an ISO 8601 compliant date string)

Format Date() in VFP report

I use database program written in VFP and I am trying to change a date format in a report generated by that program.
The current field in the report is DATE() which returns DD/MM/YYYY
I'd like the field to return DDMMYY (ie no seperators, no century and single digit days and months to have a leading zero)
Can someone tell me the report field expression I should use to achieve this?
Thanks
I solved this problem by creating a procedure and call that procedure as
date2(date(), "", 2, 2, 2) in report field.
This will print 150418
This function can also be used with default value i.e. date(date()) which will print 15-Apr-18
FUNCTION date2(par_date, par_separtor, par_day_digit, par_month_digit, par_year_digit)
LOCAL date_return, day_t, month_t, year_t
IF VARTYPE(par_separtor) <> "C"
par_separtor = "-"
ENDIF
IF VARTYPE(par_year_digit) <> "N"
par_year_digit = 2
ENDIF
day_t = IIF(DAY(par_date)<10, '0', "") + allt(str(DAY(par_date)))
month_t = PROPER(allt(left(CMONTH(par_date),3)))
year_t = right(allt(str(year(par_date))),par_year_digit)
IF VARTYPE(par_month_digit) = "N"
month_t = IIF(MONTH(par_date)<10, '0', "") + allt(STR(MONTH(par_date)))
* MESSAGEBOX(month)
ENDIF
date_return = IIF(EMPTY(par_date), " ", day_t + par_separtor + month_t + par_separtor + year_t)
RETURN date_return
ENDFUNC
Before report set the century off if it is not already (you could do this even if you only have access to report itself, either in report environment or bands code):
Set century off
Then the expression is simply:
CHRTRAN(DTOC(DATE()),'/','')
Or if you meant a date field:
CHRTRAN(DTOC(myDateField),'/','')
If, for any reason, you can't use "set century off" (and it is ON) then change the expression slightly:
Stuff(CHRTRAN(DTOC(myDateField),'/',''),5,2,'')
Maybe this one is better and works regardless of date/datetime settings:
PADL(DAY(DATE())*10000+MONTH(DATE())*100+YEAR(DATE())%100,6,'0')
(if it is a date fieldthen replace date() with the field name)

Check for dates consistency in MATLAB

Is there any straight forward way to do that? I want to give an array of dates as an input (for example 1997-01-02 1997-01-03... using the format yyyy-mm-dd) and get 1 if all the elements of the given array are consistent and 0 otherwise.
Any idea?
Here is one idea:
d = {
'1997-01-02'
'1997-01-03'
'1111-99-99'
'not a date'
}
isDateValid = false(size(d));
for i=1:numel(d)
try
str = datestr(datenum(d{i},'yyyy-mm-dd'),'yyyy-mm-dd');
isDateValid(i) = isequal(str,d{i});
catch ME
end
end
The result:
>> isDateValid
isDateValid =
1
1
0
0
The reason I do the conversion back and forth is that MATLAB will carry values outside the normal range of fields to the next one -- third example will actually be parsed as: 1119-06-07. While the last one will throw an exception
Many ways to do this using regexp. A couple of simple ones:
str = '1917-01-23';
regexp(str,'\d\d\d\d-\d\d-\d\d')
ans =
1
If the string matches exactly that pattern, you will get 1, else empty.
Or do this:
regexp(str,'-','split')
ans =
'1917' '01' '23'
Now you can verify the first piece is a valid year, the second a valid month, etc.

Ruby Date.strptime difficulties when parsing 13/Nov/11

When parsing a Jira Custom Field containing a date (e.g. 13/Nov/11) I started with this:
elsif custom.customfieldId == "customfield_10282"
#agenda_item.planned_release_date = custom.values.to_s
But the database stores it as 11/13/0011. So I got clever and used this:
elsif custom.customfieldId == "customfield_10282"
#agenda_item.planned_release_date = Date.strptime(custom.values, "%d/%m/%Y")
And now I get:
private method sub!' called for ["15/Nov/11"]:Jira4R::V2::ArrayOf_xsd_string
C:/Ruby187/lib/ruby/1.8/date/format.rb:429:in_strptime_i'
C:/Ruby187/lib/ruby/1.8/date/format.rb:401:in scan'
C:/Ruby187/lib/ruby/1.8/date/format.rb:401:in_strptime_i'
C:/Ruby187/lib/ruby/1.8/date/format.rb:601:in `_strptime'
[truncated the rest of the stack]
What am I missing?
This works:
p Date.strptime('13/Nov/11', "%d/%b/%y")
Your problems:
`%Y is the year with 4 digits (2011). Use %y
%m is the month in digits (11). Use %b instead (Nov)
If I properly understand your issue, try to use this:
Date.strptime('13/Nov/11', "%d/%b/%y") # => 2011-11-13
You have abbreviated month name and there for you should use b format directive. Beside this because your year contains only two last numbers, use y directive.

How to Find Record Set Value is Number or String?

Original:
Using VB6
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Getting Type MisMatch Error.
How To Find Record Set Value is String or Number
Exactly i need
If Number means print number like Time Format (HH:MM:SS)
else
print string value
Coding Help for the above condition
Edited Version:
I'm working with an ADO.Recordset object and am trying to determine the data type of a column at run-time. I need to handle the column value differently in my code depending on its underlying data type. If the column value is a string, I want to work with the value as-is. If it is a number, I want to treat the number as an packed time and convert it to HH:MM:SS format (i.e. the number 120537 would be converted to the string "12:05:37").
Below is some example code that demonstrates what I want to achieve. However, when I run this code I get a "Type Mismatch" error:
If rsCardEvent(4).Value = Str Then
TimOut = rsCardEvent(4)
Else
TimeOut = Left(TimOut, 2) & ":" & Mid(TimOut, 3, 2) & ":" & Right(TimOut, 2)
End If
Have a look at the Visual Basic 6 function library. There are functions that can help you determine the underlying type of a value.
There are quite a few but you might find these useful:
IsNumeric
IsDate
Based on this article, if rsCardEvent is an ADO recordset, you could check the Type property. Something like this:
Select Case rsCardEvent(4).Type
Case adBSTR, adChar, adVarChar, adWChar, _
adVarWChar, adLongVarChar, adLongVarWChar
' It is a string '
Case Else
' It is not a string '
End Select
You can use the IsNumeric function available in VB6.
How about:
If TypeName(rsCardEvent(4).Value) = "String" then
http://msdn.microsoft.com/en-us/library/5422sfdf.aspx

Resources