473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Read XML date (d/M/y) format & convert to date object

31
Hi,

I have some XML data which contains a date in dd/mm/yyyy format (ie UK date format).

In javascript I need to read this date then calculate the age (in days& decimals) of the XML data.

I can get today's date in milliseconds:
Expand|Select|Wrap|Line Numbers
  1. var current_date = new Date();
  2. var date1_ms = current_date.getTime();
but I can't see how to read the XMLdate in UK format, I have the XML date (string) in a var called docdate & I thought this should work:

Expand|Select|Wrap|Line Numbers
  1.    var date2 = Date.parseString(docdate,"d/M/yyyy");
  2. var date2_ms = date2.getTime();
Then I can sutract date1_ms from date2_ms to get the age in milliseconds, but the Date.parseString(docdate,"d/M/yyyy"); doesnt work. and my alternative of:
var date2=Date/parse(docdate."d/M/yyyy")
ignores my format command and reads the date as US format.

Can any one give me the correct line oif code, or a better one:-) or point me to a reference. Everything I;ve found formats the date for OUTPUT rather than INPUT.
Thanks.
Sep 12 '07 #1
6 3228
pbmods
5,821 Expert 4TB
Heya, Gretsch.

To output a date in a custom format, you'll have to use Date::getYear(), Date::getDay(), etc. and manually build the string piecemeal.
Sep 12 '07 #2
Gretsch
31
Thanks pbmods,

...but I assume Date::getMonth() would get the first 2 digits of my dateString (in the mistaken belief that it was in US date format)
.. or have I misunderstood your suggestion?
Sep 13 '07 #3
pbmods
5,821 Expert 4TB
Heya, Gretsch.

Good point.

To do that, you will have to create an array and reference its index. E.g.,
Expand|Select|Wrap|Line Numbers
  1. var months = ['Jan', 'Feb', 'Mar', ..., 'Dec'];
  2. alert(months[theDate.getMonth() - 1]);
  3.  
Sep 13 '07 #4
mrhoo
428 256MB
the getMonth and other Date methods don't work on strings, but only on Date objects. You don't need to worry about reading a date, but about the way you create the Date from your string input.

If you are using a dd/mm/yyyy formatted string you can assemble the Date by splitting the string on non digits and using the returned array elements-
Expand|Select|Wrap|Line Numbers
  1. var dString= '01/08/2007';
  2. var dArray= dString.split(/\D+/);
  3. var dDate= new Date(dArray[2],dArray[1]-1,dArray[0]);
Sep 14 '07 #5
Gretsch
31
Excellent - thanks very much
Sep 14 '07 #6
pbmods
5,821 Expert 4TB
Heya, Mr. Hoo.

Please use CODE tags when posting source code:

[CODE=javascript]
JavaScript code goes here.
[/CODE]
Sep 14 '07 #7

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

Similar topics

15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
3
by: J. Muenchbourg | last post by:
while inserting new records into SQL, i'm using the folloinwg sqlstatement> Dim MySQL as string = "Insert into roster (pname, pnotes, thedate) values (@pname, @pnotes, @thedate)" the sql...
5
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
by: [Yosi] | last post by:
DateTime dd = DateTime.ParseExact ("27/09/2003", "dd/MM/yyyy"); I still get an error : "(939): No overload for method 'ParseExact' takes '2' arguments" >-----Original Message----- >Dates...
4
by: jty202 | last post by:
I have string that contains a date in this format (14-Jan-05). I want to store in date object if theres one and access each part of the date (month, year, dates, day of week). Specifically I...
12
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
12
by: Yannick | last post by:
Hi, I've got a problem accessing a ms-access db with a sql statement like this: SELECT * FROM laTable WHERE laDate = #05/21/2004# ; with asp.net (vb code) laTable contains a "laDate"...
7
by: JamesG | last post by:
Hi, I need to convert the current time to the format "hh:mm:ss" and date to the format "yyyy-MM-dd". The result of both conversions needs to be of System.DateTime format. I've tried using...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.