473,761 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert date format

chandru8
145 New Member
I read the date from Notepad and convert it into date using Dateserial function. I try to convert it into my desired format like fomat( ,"dd/mm/yy") but I am unable to do that.
Expand|Select|Wrap|Line Numbers
  1.    Dim fs As New FileSystemObject
  2.     Dim ts As TextStream
  3.     Dim str As String
  4.     Dim int1 As Integer
  5.     Dim dt1 As String
  6.     Dim dt As Date
  7.     Dim date1 As Date
  8.     Dim DATE2 As Date
  9.  
  10.     Set ts = fs.OpenTextFile("c:\1.txt", ForReading, True)
  11.     str = ts.ReadLine
  12.     dt1 = Trim(Mid(str, 1, 10))
  13.     date1 = DateSerial(Year(dt1), Month(dt1), Day(dt1))
  14.     DATE2 = Format(date1, "mm/dd/yy")
  15.  
  16.  
  17.     Set fs = Nothing
  18.     ts.Close
Sep 26 '07 #1
7 2080
shrimant
48 New Member
Expand|Select|Wrap|Line Numbers
  1. Set ts = fs.OpenTextFile("c:\1.txt", ForReading, True)
  2. str = ts.ReadLine
  3. dt1 = Trim(Mid(str, 1, 10))
  4. date1 = Cdate(dt1)
  5. DATE2 = Format(date1, "mm/dd/yy")
Serial is to convert a redable date to serial. see the code changes above..should work.

Or else use

FormatDateTime( dt1, <DateTimeEnumer ation>)

DateTimeEnumera tion
vbGeneralDate For real numbers, displays a date and time. If the number has no fractional part, displays only a date. If the number has no integer part, displays time only. Date and time display is determined by your computer's regional settings.
vbLongDate Displays a date using the long-date format specified in your computer's regional settings.
vbShortDate Displays a date using the short-date format specified in your computer's regional settings.
vbLongTime Displays a time using the long-time format specified in your computer's regional settings.
vbShortTime Displays a time using the short-time format specified in your computer's regional settings.

Regards
-Chanda Patel-
[link removed]
Sep 26 '07 #2
Killer42
8,435 Recognized Expert Expert
I read the date ...
I don't see how Notepad has anything to do with this.

Anyway, allow me to try and explain the situation a bit. The Year(), Month() and Day() functions all accept a date value as a parameter, and extract the relevant piece of information from it. So you can't use them unless you already have a date value. What you have at that point is a string (which hopefully looks like a date). You need to use something like the DateValue() function to convert the string to a date.

Conversely, the Format() function is used to format a value (such as a number or a date) into a string, generally for display purposes. Keep in mind that dates are not stored in any particular format. They are just stored as a number. You use things like Format() to convert the number to whatever format you want to see at the time.

Try this version...

Expand|Select|Wrap|Line Numbers
  1. Dim fs As New FileSystemObject
  2. Dim ts As TextStream
  3. Dim strng As String
  4. Dim dt1 As String
  5. Dim date1 As Date
  6. Dim Date2 As String
  7.  
  8. Set ts = fs.OpenTextFile("c:\1.txt", ForReading, True)
  9. strng = ts.ReadLine
  10. dt1 = Trim(Mid(strng, 1, 10))
  11. date1 = DateValue(dt1)
  12. Date2 = Format(date1,"mm/dd/yyyy")
  13. ' Note, it would be better to use something like this...
  14. Date2 = Format(date1,"short date")
  15.  
  16. ts.Close
  17. Set ts = Nothing
  18. Set fs = Nothing
  19.  
Note that I changed the variable name str, since Str is a reserved word (it's the name of a function to convert a numnber to a string).
Sep 26 '07 #3
chandru8
145 New Member
hi
thanks for your reply

i tried with both the codes, after getting the date value from the flat file i couldn't change to my desired format.
Sep 27 '07 #4
Killer42
8,435 Recognized Expert Expert
I tried with both the codes, after getting the date value from the flat file i couldn't change to my desired format.
Keep in mind that from VB's viewpoint what you are getting from the file is not a date value, but a string. To treat it as a date, you have to convert it.

Can you show us what you have now, and explain what the trouble is with it? I mean, do you get an error, or just unexpected results, or what?
Sep 27 '07 #5
chandru8
145 New Member
hi
for this 27/09/07 iam getting 9/7/2027 ( 27/09/07 is from notepad)

do you have any code also please send it its very urgent.
Sep 28 '07 #6
QVeen72
1,445 Recognized Expert Top Contributor
Hi

How about this :

Date2 = Format(cdate("2 7/09/07"),"dd-mm-yyyy")


Date2 = Format(cdate(dt 1),"dd-mm-yyyy")

REgards
Veena
Sep 28 '07 #7
Killer42
8,435 Recognized Expert Expert
for this 27/09/07 iam getting 9/7/2027 ( 27/09/07 is from notepad)
So, it's interpreting the value as being in either YY/MM/DD or YY/DD/MM format - there's no way to tell which from your example.

Have you checked your date format in Windows Regional Settings? I mean, if you have a bunch of numbers with slashes between them, Windows has to take its best guess as to what is where. To me 9/7/2027 would mean the 9th of July, while to an American it would be the 7th of September.

If all else fails, just chop up the string (Split() function can do this for you) into three parts, and feed the three pieces into the DateSerial() function. Since it has separate Year, Month and Day parameters, there's no way (hopefully) that it can misinterpret them.
Sep 28 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
39805
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear = (String)sail_date.toString().substring(0,4);
4
5390
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
5
16334
by: goochey | last post by:
I'm trying to convert a Julian Date (Format "4365") into an actual calendar date in Visual Basic, can anyone help me out with this.
2
5087
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings set to "en-UK" and date to
12
3859
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
1
4606
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as "dd/MM/yyyy" input as "01082003" convert to date value as, 01 Aug 2003 Example, User Defined Date Format as "yyyy,dd,MM"
10
162679
by: bonnie.tangyn | last post by:
Dear all In my ASP page, user can enter date in dd/mm/yyyy format. How can I use Javascript to convert dd/mm/yyyy to yyyy-mm-dd hh:mm:ss. Please give me some advices. Cheers Bon
2
16807
by: thewilldog | last post by:
Hello, I've reviewed the archives here to address the issue, but I'm still running into problems. I've got a table field populated with the record date in text "YYYYMMDD" To convert it into a recognizable date format, I've done the following: Query 1: References Source Table; Isolates Year, Day; creates MMDD field Acc Open Year: Left(,4) Acc Open Day: Right(,2) MMDD: Right(,4) Query 2: References Query 1; Isolates Month
4
30265
by: Ashraf Ansari | last post by:
Hi, How Can I convert MM/dd/yyyy format into dd/MM/yyyy and the date which is converted into dd/MM/yyyy should be in DateTime format. I do not want to store it as a string. Please help Thanks in advance
4
4528
by: thomasc1020 | last post by:
This is regarding VB.NET 2003. Variable 'Date' is a string and it contains date information in this format: "DEC/05/2007". Now I am trying to convert the format as "2007-12-05". Is it possible to convert the arrangement using 'Format' method? If so, please educate me how I can do such operation. Thank you!
0
9353
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.