How to get a date to stay the right format

Graeme
Graeme used Ask the Experts™
on
Hi all, just wondering how I get the date to stay in the same format, no default

dd / mm / yy - this is the format I want to use

using ASP Classic and MS Access 2003

example date: 1/10/17
-everytime i update this is changes from 1/10/17 to 10/1/17 and then back.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Big MontyWeb Ninja at large

Commented:
you can use the FormatDateTime function, but that'll still give a for character year, so maybe a custom function would be better:

function customFormatDate( d )
     customFormatDate = padZero( Day( d ) & "/" & padZero( Month( d ) & "/" & Right( padZero( Year( d ), 2 )
end function

Open in new window


and you would call it like:

Response.Write customFormateDate( now )
Graemewebber4technologies

Author

Commented:
4 character year is fine for me, FormatDateTime ill try out and get back to you

cheers
Big MontyWeb Ninja at large

Commented:
ok, it should work the way you want to as long as your server date format settings are dd/mm/yyyy.

example:

Response.Write FormatDateTime( now, 2 )
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Graemewebber4technologies

Author

Commented:
Just looking at FormatDateTime here: http://www.w3schools.com/asp/func_formatdatetime.asp

not seeing dd / mm / yyyy ??

I only see mm / dd / yyyy
Big MontyWeb Ninja at large

Commented:
I believe the format is based off of your local servers date/time locale settings. Give it a shot and see what happens!
Graemewebber4technologies

Author

Commented:
Still does the same

would I have to put it on the Response.Write ? or the Executed Query? or both?

I currently have it on the Executed Query
Graemewebber4technologies

Author

Commented:
also tried putting it on the Response.Write, same thing too
Big MontyWeb Ninja at large

Commented:
I would set it as its own variable, then use that in your query.

Did you try the custom function I gave you?
Graemewebber4technologies

Author

Commented:
would it matter with the date...

<input type=text> put in the there and submitted, when you go back to update it, that is when it changes, and you can keep clicking update and it changes around
Big MontyWeb Ninja at large

Commented:
can you post your updated code?
Graemewebber4technologies

Author

Commented:
oConn.Execute("UPDATE events SET event_name='"&Request.Form("eventname")&"',event_start=#"&FormatDateTime(Request.Form("eventstart"),2)&"#,event_finish=#"&FormatDateTime(Request.Form("eventfinish"),2)&"#,event_location="&GPLocations("location_ID")&",event_code='"&Request.Form("codeID")&"',event_conf="&EventConf&",event_live="&EventLive&" WHERE event_ID="&Request.QueryString("event"))

Open in new window

Big MontyWeb Ninja at large

Commented:
if you do:

Response.Write "UPDATE events SET event_name='"&Request.Form("eventname")&"',event_start=#"&FormatDateTime(Request.Form("eventstart"),2)&"#,event_finish=#"&FormatDateTime(Request.Form("eventfinish"),2)&"#,event_location="&GPLocations("location_ID")&",event_code='"&Request.Form("codeID")&"',event_conf="&EventConf&",event_live="&EventLive&" WHERE event_ID="&Request.QueryString("event"))

then, whatever is outputted to the screen, run directly in your database and see if you get any results
Graemewebber4technologies

Author

Commented:
have been running it in my database, however because it is an UPDATE query, it doesnt produce a result?

however when it updates the DB, it changes it back and forth from 1/10/2017 to 10/1/2017 and vv
Big MontyWeb Ninja at large

Commented:
even when using my custom function?

oConn.Execute("UPDATE events SET event_name='"&Request.Form("eventname")&"',event_start=#"&customFormatDate(Request.Form("eventstart"))&"#,event_finish=#"&customFormatDate(Request.Form("eventfinish"))&"#,event_location="&GPLocations("location_ID")&",event_code='"&Request.Form("codeID")&"',event_conf="&EventConf&",event_live="&EventLive&" WHERE event_ID="&Request.QueryString("event"))
Graemewebber4technologies

Author

Commented:
going to try the custom function now
webber4technologies
Commented:
just having a look at some other code I have...

the difference between  ' " & 1/10/2017 & " ' and # " & 1/10/2017 & " # ??
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
It should read using Format:
oConn.Execute("UPDATE events SET event_name='"&Request.Form("eventname")&"',event_start=#"&Format(Request.Form("eventstart"),"yyyy\/mm\/dd")&"#,event_finish=#"&Format(Request.Form("eventfinish"),"yyyy\/mm\/dd")&"#,event_location="&GPLocations("location_ID")&",event_code='"&Request.Form("codeID")&"',event_conf="&EventConf&",event_live="&EventLive&" WHERE event_ID="&Request.QueryString("event"))

Open in new window

/gustav
Graemewebber4technologies

Author

Commented:
Hi Gustav, this will insert the date as dd / mm / yyyy ?
Big MontyWeb Ninja at large

Commented:
Format() won't work, it's a visual basic function, not vbscript.

To answer your previous question, you should surround the date with pound signs
Graemewebber4technologies

Author

Commented:
Ah ok, I know I have a couple of other statements with the single quotes, but they are INSERTS
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Well, what do you see?

I don't know what you read from the form. You may have to apply DateValue as well:

    event_finish=#"&Format(DateValue(Request.Form("eventfinish")),"yyyy\/mm\/dd")&"#,

/gustav
Big MontyWeb Ninja at large

Commented:
again, there is no FORMAT() function in classic asp, you need to use formatDateTime(), as I suggested earlier. or use the custom function I gave you.

Did you try that? what were your results? I can't help you if you don't respond to the questions/solutions I propose
Graemewebber4technologies

Author

Commented:
Hi BigMonty, sorry I was trying to figure out where the missing brackets went, got that sorted.

Error now:

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'padZero'

/admin2.asp, line 9 -> customFormatDate = padZero( Day( d ) )& "/" & padZero( Month( d ) )& "/" & Right( padZero( Year( d ), 2 ))

Open in new window

Big MontyWeb Ninja at large

Commented:
dd this function anywhere on your page:

    function padZero( n )
        if CInt( n ) < n then
            padZero = "0" & n
        else
            padZero = n
        end if
    end function
Graemewebber4technologies

Author

Commented:
I have just tried replacing # with quotes and it inserts and doesn't change on recall and update... its working... ? is this supposed to happen? with quote and not # ?
Big MontyWeb Ninja at large

Commented:
the best way to tell if it's working is to see if the data is there in the database and in the format you want :)
Graemewebber4technologies

Author

Commented:
yeah its all working great, no issues at all

why would there be a difference?
Big MontyWeb Ninja at large

Commented:
not sure why, both formats should have worked
Graemewebber4technologies

Author

Commented:
Interesting... how do we close off this question now?

I mean you have been a help is debugging it and trying different stuff...
Big MontyWeb Ninja at large

Commented:
i can't tell you how to close a question, I think EE may object to that :)

if any of my answers helped you and you think they may be useful to others, then select them. if not, delete the question and it'll auto-delete in 4 days.

FYI - I'm not here for the points, so as long as you got the solution, I don't care how :)
Graemewebber4technologies

Author

Commented:
tried something not suggested and worked

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial