473,386 Members | 1,705 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,386 software developers and data experts.

Working with Date

Helo.... How I can convert string "26/03/2006 15:51" for a date? I need
to convert and to compare if "09/06/2006 14:20" is lesser or equal that
the current date. Everything in Javascript.

Mar 26 '06 #1
4 3724
"blini" <fe**********@gmail.com> writes:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need
to convert and to compare if "09/06/2006 14:20" is lesser or equal that
the current date. Everything in Javascript.


Try this:
---
function stringToDate(string) { // expects format "dd/MM/yyy hh:mm"
var match = /^(\d\d)\/(\d\d)\/(\d{4}) (\d\d):(\d\d)$/.exec(string);
if (match) {
return new Date(match[3],match[2]-1,match[1],match[4],match[5]);
}
}
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Mar 26 '06 #2
blini wrote:
Helo.... How I can convert string "26/03/2006 15:51" for a date? I
need to convert and to compare if "09/06/2006 14:20" is lesser or
equal that the current date. Everything in Javascript.


You can write a specific solution as Lasse suggested, or you can also use
generalized functions for parsing and comparing like those at
http://www.JavascriptToolbox.com/lib/date/

Personally, I prefer the latter ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Mar 27 '06 #3
JRS: In article <11**********************@z34g2000cwc.googlegroups .com>
, dated Sun, 26 Mar 2006 10:58:58 remote, seen in
news:comp.lang.javascript, blini <fe**********@gmail.com> posted :
Helo.... How I can convert string "26/03/2006 15:51" for a date? I need
to convert and to compare if "09/06/2006 14:20" is lesser or equal that
the current date. Everything in Javascript.


Consider

DS = "26/03/2006 15:51" // UK, EU, etc. field order

function Cvt(ds) {
return new Date(ds.replace(/(..).(..).(....)/, "$3/$2/$1")) }

D = Cvt(DS)

Cvt will convert, presuming exact format, to YYYY/MM/DD hh:mm and then
use new Date() which should be and seems to be everywhere safe with that
string format. You could convert to MM/DD/YYYY, which is unreasonable,
may be safe, and would fail if browsers localised properly. OTOH if
they localised properly you might not want to change the field order.

Your question, as posed, has a problem for one hour in the Autumn in
many countries, where civil time reverses. The code will give the
obviously-correct answer; but that is not necessarily the right one.
I *think* Lasse's method (more foolproof; but that class of fool appears
to be an empty set) would be quicker with new Date(Date.UTC(...)) ,
and its result equally usable.

In my system,
new Date(DS+" GMT") is several times faster than new Date(DS) .

Speed is, however, unlikely to matter to you.
Having done the conversions to Date Objects, compare by subtraction,
which does implicit .valueOf() .

So test is Cvt(DS) <= new Date() if by current date you mean
current date/time. If you want the start of today, which is the true
current date, use .setHours(0,0,0,0) .

Read the newsgroup FAQ.
NOTE : if you need to compare more than one such input with the current
date[/time], then call new Date() once and store the result for
repeated use.

NOTE : if you need to compare many such inputs with the current
date[/time], then call new Date() once, convert it to YYYYMMDDhhmm
format, and store that; omit "new Date"...")" from Cvt and change the
RegExp to give YYYYMMDDhhmm - string comparison is then all that is
needed - but insert a unary + so that Cvt returns a number, since
numerical comparison should be faster.

with (new Date()) YYYYMMDDhhmm =
(((getFullYear()*100 + getMonth()+1)*100 + getDate())*100 +
getHours())*100 + getMinutes()

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 27 '06 #4
<script language="javascript">
function compare_date (start,end)
{
if (start != '' && end != '' )
{
var dateStart = new Date(parseInt(start.substring(6,10)),parseInt(star t.substring(3,5), 10)-1,parseInt(start.substring(0,2), 10),parseInt(start.substring(11,13), 10),parseInt(start.substring(14,16), 10),parseInt(start.substring(17,19), 10));//new Date(start);
var dateEnd = new Date(parseInt(end.substring(6,10)),parseInt(end.su bstring(3,5), 10)-1,parseInt(end.substring(0,2), 10),parseInt(end.substring(11,13), 10),parseInt(end.substring(14,16), 10),parseInt(end.substring(17,19), 10));//new Date(start);

if (dateEnd > dateStart)
{
return true
}
else
{
return false
}
}

}

</script>
May 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Treetop | last post by:
I cannot get this array to work. I want to have the game listed until the day after the event, then come off the list. function events() { var today = new Date(); var dayarray=new...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
0
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
1
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
8
by: Hardy Wang | last post by:
Hi: Is it possible for me to create/open web application from remote machine other than port 80? And create application directly under virtual web site instead of creating a virtual directory?...
3
by: Jason Huang | last post by:
Hi, In our C# Windows Form application, we are using the SQL Server 2000 as the database server. The Database table MyTable has a field RegistrationDate which represents the Date a client comes...
0
by: kumarkanth | last post by:
My question is how do I manipulate the procedure to calculate cut-off date with in working days and it should skip weekends and holidays? I have holiday table to determine holidays Table Name :...
31
by: ajos | last post by:
hi frnds, i have a form,which has 2 input text boxes, the values are entering the text boxes,when i leave the 2 text boxes blank and hit submit a java script gives the message that the 2 fields are...
4
by: jmarcrum | last post by:
Hi everyone!! I have a continuous form that allows users to select a record and chnage the DATE that the record is assigned to another DATE within the same year. The button is called "Change plan...
11
by: lenygold via DBMonster.com | last post by:
Hi everybody! This query is supposed to count consecutive years from the current year without OLAP. Input Table: ID DateCol 1 02/01/2006 1 01/01/2006 1 01/01/2005
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.