User defined date format in VBA Access - ms-access

I can't figure out how to tell VBA the correct date format, as it seems to assume in the wrong way around. So CDate("24/02/2016 10:59") is giving the correct date - dd/mm/yyy. However when it iterates through a date like CDate("01/03/2016 00:59") - it assumes number 03 is not a month but a day so VBA is assuming it's mm/dd/yyyy, so it's giving me a date 03/01/2016 which is wrong and messing my data. So how can I tell VBA it's actually dd/mm/yyyy format. VBA seems to automatically pick up nicely even if it's "2016/01/14", but it's failing when it's not really obvious which part of numbers are months e.g. both dd and mmare less than 12.
I'm reading this date from a CSV file using WS.Cells(irow, icol).Value.
This is what I tried so far:
datDate = CDate(Format(WS.Cells(iRow, iCell).Value, "dd-mmm-yyyy hh:mm:ss"))

When the CDate() function encounters an ambiguous "aa/bb/yyyy" date string it will be interpreted according to the order that the Month and Day appear in the short date format defined by the Regional Settings in Windows.
For example:
When my machine is set to "English (United States)" with a short date format of M/d/yyyy then Month(CDate("02/04/2016")) returns 2.
However, after changing my Regional Settings to "English (Canada)" with a short date format of dd/MM/yyyy then Month(CDate("02/04/2016")) returns 4.
Note that this is different from the behaviour of the Jet/ACE database engine when interpreting ambiguous #aa/bb/yyyy# date literals in SQL statements. In that case it will always interpret them as mm/dd/yyyy regardless of the regional settings.
The best solution, as always, is to ensure that the string representation uses an UNambiguous date format, e.g., 2016/02/04 will always be interpreted as Feb 4.

Use mm/dd/yyyy hh:nn:ss like:
#03/01/2016 16:32:58#
or DateSerial and TimeSerial:
DateSerial(2016, 03, 01) + TimeSerial(16, 32, 58)
To parse the string, use Mid:
TrueDate = Dateserial(Mid(s, 7, 4), Mid(s, 4, 2), Mid(s, 1, 2)) + TimeSerial(Mid(s, 12, 2), Mid(s, 15, 2), 0)
s = "01/03/2016 00:59"
' Returns: 2016-03-01 00:59:00
s = "24/02/2016 10:59"
' Returns: 2016-02-24 10:59:00

Change the format of the date before you convert. Something like this;
Dim strDate As String, strFormatDate As String
Dim dte As Date
strDate = "01/03/2016 00:59"
strFormatDate = Format(strDate, "dd-mmm-yyyy hh:nn")
dte = CDate(strFormatDate)
This changes the date string to 01-Mar-2016 00:59 which should convert to a date data type without confusion.

Related

extract date from file name using SSIS expression

HT,
Using Microsoft SSIS.
I have a input CSV file called - LatLong_WD_Locations_06-21-2021.
I want to extract date from this file name in 20210621 format using SSIS expression and store in to a variable called v_FileDate which is Int32 type.variable v_FileName is a string type.
I tried with
(DT_I4) REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "_", 2)+1,10),"-","")
but its not working.
Can I have some help here, please.
TIA
Almost there. You specified 2 for the occurrence argument in FINDSTRING expression. So it is finding the _ before Location in the file name giving you a result of Locations_. Since that is not a integer it is throwing an error.
Change the 2 to a 3:
(DT_I4) REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "_", 3)+1,10),"-","")
The above would account for if the V_FileName has a file extension. It would not get you the final format of yyyyMMdd. See below...
You could also simplify and use RIGHT expression. Get the right 10 characters of the string and then replace:
(DT_I4) REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-","")
I updated the above statement to account for if v_FileName had an extension. That still does not give your final format of yyyyMMdd. See below...
Those 2 expressions above will get the date out of the v_FileName, but in format MMddyyyy. Now you will have to parse out each part of the date and put it back together using one of the above statements. The example below is using the one with RIGHT:
(DT_I4) SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 5,4)
+ SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 1,2)
+ SUBSTRING(REPLACE(RIGHT(REPLACE(#[User::v_FileName], ".csv",""), 10),"-",""), 3,2)
If you ever have Date on the last 10 positions in file name solution is very simple. But if that is not case, write me below in a comment and I will write a new expression.
Solution explained step by step:
Get/create variable v_FileDate with value LatLong_WD_Locations_06-21-2021
For check create DateString with expression
RIGHT( #[User::v_FileDate], 4) +
LEFT (RIGHT( #[User::v_FileDate], 10), 2) +
LEFT (RIGHT( #[User::v_FileDate], 7), 2)
Create a final variable DateInt with expression
(DT_I4) #[User::DateS]
How variable should like:
Or you can with a single variable (It would be better with a single variable).
(DT_I4) (
RIGHT( #[User::v_FileDate], 4) +
LEFT (RIGHT( #[User::v_FileDate], 10), 2) +
LEFT (RIGHT( #[User::v_FileDate], 7), 2)
)
Final Result
Using script task...
Pass in v_FileDate as readwrite
Pass in v_FileName as readonly
Code:
//Get filename from SSIS
string fileName = Dts.Variables["v_FileName"].Value;
//Split based on "_"
string[] pieces = fileName.Split('_');
//Get the last piece [ref is 0 based and length is actual]
string lastPiece = pieces[piece.Length -1];
//Convert to date
DateTime d = DateTime.ParseExact(lastPiece, "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture);
//Convert to int from string converted date in your format
Dts.Variables["v_FileDate"].Value = int.Parse(d.ToString("yyyyMMdd"));
Some work around -Working for me in (YYYYMMDD) format.
Thanks #keithL
(DT_I4)( REPLACE(RIGHT(#[User::v_FileName], 5),"-","")+REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "", 3)+1,2),"-","")+REPLACE(SUBSTRING( #[User::v_FileName],FINDSTRING( #[User::v_FileName] , "", 3)+3,4),"-",""))

How can I get the timestamp in “yyyy/MM/dd hh:mm:ss.fff"” format in VBA?

I want to get this output:
2018-09-02 00:00:00.000
I tried the below code:
.Cells(LRS + 1, 15).Value = Format(.Cells(LRS + 1, "A").Value, "yyyy-MM-dd hh:mm:ss.fff")
And I got:
2018-09-02 00:00:00.fff
The initial date in Excel has the following format yyyy/mm/dd, no time included. That's why the time part includes only zeros 00:00:00.000. The reason I want to include the time in the specific format is that I'm planning to import those dates into a SQL table with that format.
Is there any solution?
As you can see from the documentation fff is not recognised as a formatting token in VBA.
Helpfully, you can actually import your data into SQL without formatting it to add the time. If you import it into a datetime field the SQL engine will automatically default the time part of the field to midnight on the date you give it.
I think you can just change your format string to yyyy-MM-dd by itself.
However if you really want to do it like this, then since there's no time specified then just hard-code 000 instead of fff. The rest of the time can be similarly hard-coded, since it never varies, so you end up with yyyy-MM-dd 00:00:00.000. But as I said, I think it's a bit pointless.
After replacing the cell format with the corresponding format, it is likely that the value of the cell is imported as text, not as a value.
Sub test()
Dim s As String, s1 As String, s2 As String
'First Cell format as your "yyyy-mm-dd hh:mm:ss.000"
Range("a2").NumberFormatLocal = "yyyy-mm-dd hh:mm:ss.000"
'In vb,This format("yyyy-mm-dd hh:mm:ss.000") is not recognized.
Range("b2") = Format(Range("a2"), "yyyy-mm-dd hh:mm:ss.000")
s1 = Format(Range("a2"), "yyyy-mm-dd hh:mm:ss.000")
's1 = "2018-09-03 01:24:33.000"
'Since you format a2 cell as "yyyy-mm-dd hh:mm:ss.000" you can get data as text
Range("b2") = Range("a2").Text
s2 = Range("a2").Text
's2= "2018-09-03 01:24:33.240"
End Sub
Sheet Data
Local Window

Milliseconds to DateTime in WP8 not working

I need to convert a date in milliseconds to a user readable date and I can't.
My date is: 1494519599999 which corresponds to: Thu May 11 2017 18:19:59 GMT+0200
I need to get something like dd/mm/yyyy HH:mm:ss
I have tried what I read in other stackoverflow posts:
DateTime date = new DateTime(long.Parse(ticks));
date.ToString("yyyy-MM-ddThh:mm:ssZ");
But I always get 0 for year, month and more. I tought I should multiply or divide by 1000 the number but it was worst.
What is the correct way to do this?
The timestamp seems to be the value often used in JavaScript to represent a date (the number of milliseconds elapsed since January 1 1970). You can convert that to a DateTimeOffset using this code:
var timestamp = 1494519599999;
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
var dateTimeOffset = epoch.AddMilliseconds(timestamp);
The result is 05/11/2017 16:19:59 +00:00.
I am using a DateTimeOffset to make it easier for you to change the offset from +0000 to +0200. You can change the offset using this code where I assume you want to convert to local date and time. Windows Phone 8 does not support the full functionality of the TimeZoneInfo class so if you need to convert to another time zone you need create code the explicitely changes the offset:
dateTimeOffset = dateTimeOffset.ToLocalTime();
Now the result is 05/11/2017 18:19:59 +02:00.
If you prefer to use DateTime instead of DateTimeOffset you can use this code to create a UTC DateTime:
var timestamp = 1494519599999;
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var dateTime = epoch.AddMilliseconds(timestamp);
The result is 05/11/2017 16:19:59 and if your local timezone has a offset of +0200 you can convert it to a local DateTime to get the desired result. However, when time zones are involved I suggest that you use DateTimeOffset.
Try this:
var time = TimeSpan.FromMilliseconds(milliseconds);

C - How to convert time_t to tm?

I have a variable which using time_t data type. I want to convert this type into "YYYY-MM-DD HH:MM:SS". I just know if it's only works in localtime() example:
char buff[20];
time_t now = time(NULL);
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));
any suggestion how to convert it? because I have the time which always increased every minute, not the fixed one like the localtime(). I need this conversion for matching with datetime type in MySQL database.
The functions gmtime and localtime (for UTC and local time respectively) will turn an arbitrary time_t into a struct tm with individual fields for year, month and so on.
You can then just use strftime as you have, or sprintf to create strings from them, such as with:
char buff[11];
sprintf (buff, "%04d-%02d-%02d",
mytm.tm_year + 1900,
mytm.tm_mon + 1,
mytm.tm_mday);

DateField: selectedDate shows hours as 24:00:00

With a DateField component, the selectedDate.getHours returns as 24:00:00. I want it to return as 00:00:00.
Is there an easy way to do this?
Thanks!
UPDATE:
First, I set a variable in my Model that equals the selectedDate of a DateField component:
model.generalInfo.endDate = endDate_df.selectedDate;
Then I set another variable based on that value and I trace it out:
param.todate = df.format( model.generalInfo.endDate.toString() );
And this is where I see the time equal to 24:00:00
you could try something like
selectedDate.time = selectedDate.time - 24 * 60 * 60 * 60 * 1000
as a Date.time represents miliseconds since 1970 or whatever.. you substract 24 hours..
if it not works for you, you can create a new function or getter that converts it, or you can create a new mxml module, with DateField as superclass, and you can override the getHours method. tons of options to do this..
It looks like you are using the Flex DateFormatter to format the Date object. Have a look at the docs for this class, it has a formatString property that you can use to control how to output the date (or in this case the time).
If you give the DateFormatter a format string that contains "H" will output the hour in 24 hour format using the number range 1-24. If the format string contains "J" it will output the hour in 24 hour format using the range 0-23.
To get your desired output, use "JJ" in the format string, in addition to any other items. For example to output the hours, minutes, seconds:
var someDate:Date = new Date(2012, 11, 5);
var df:DateFormatter = new DateFormatter();
df.formatString = "JJ:NN:SS";
var formatted:String = df.format(someDate); // 00:00:00
Also, as #Flextras mentioned, there is Flash localization API you can use which has the added benefit of converting date/time strings to the values used by their locale. These are the DateTimeFormatter classes:
fl.globalization.DateTimeFormatter
spark.formatters.DateTimeFormatter (Flex 4)
These classes format dates into the user's locale (or one that you specifcy), and format Date objects the same way the DateFormatter does.