?
Solved

Change Date Format in ASP

Posted on 2008-11-14
6
Medium Priority
?
554 Views
Last Modified: 2012-05-05
How do I chane the date from 11/12/2008 to 20081112 ?

THanks
0
Comment
Question by:wellsuited
  • 4
  • 2
6 Comments
 
LVL 1

Expert Comment

by:_savage
ID: 22959828
Are you trying to do this on the fly in a page or is the date coming from a database?
0
 
LVL 1

Accepted Solution

by:
_savage earned 2000 total points
ID: 22960012
If you are looking for straight vbscript, then could use this. I fyoua re pulling from SQL, let mek now and I've a UDF you could use in the source query.
MsgBox(iso_date("11/12/2008"))
 
function iso_date(byval dt)
dim y: y = year(dt)
dim m: m=month(dt)
dim d: d=day(dt)
 
  if m < 10 then m="0" & m
  if d < 10 then d="0" & d
  
  iso_date = y & m & d
end function

Open in new window

0
 
LVL 1

Author Comment

by:wellsuited
ID: 22960122
This would be coming from the database.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:_savage
ID: 22960319
ok, well,  you could use the function I posted and pass the date variable in the page

or

below is the UDF (I am assuming you are using MS SQL Server). Its a sledgehammer for this, but as you can see, it accomodates a great many date and time conversions.

in the source query, you would call it like this:

SELECT dbo.FORMAT_DATETIME(YOURDATEFIELD, 'YYYYMMDD') as MYDATEVARNAME
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[FORMAT_DATETIME] 
( 
    @dt DATETIME, 
    @format VARCHAR(16) 
) 
RETURNS VARCHAR(64) 
AS 
BEGIN 
    DECLARE @dtVC VARCHAR(64) 
    SELECT @dtVC = CASE @format 
 
    WHEN 'LONGDATE' THEN 
        DATENAME(dw, @dt) 
        + ',' + SPACE(1) + DATENAME(m, @dt) 
        + SPACE(1) + CAST(DAY(@dt) AS VARCHAR(2)) 
        + ',' + SPACE(1) + CAST(YEAR(@dt) AS CHAR(4)) 
    WHEN 'LONGDATEANDTIME' THEN 
        DATENAME(dw, @dt) 
        + ',' + SPACE(1) + DATENAME(m, @dt) 
        + SPACE(1) + CAST(DAY(@dt) AS VARCHAR(2)) 
        + ',' + SPACE(1) + CAST(YEAR(@dt) AS CHAR(4)) 
        + SPACE(1) + RIGHT(CONVERT(CHAR(20), 
        @dt - CONVERT(DATETIME, CONVERT(CHAR(8), 
        @dt, 112)), 22), 11) 
    WHEN 'SHORTDATE' THEN 
        LEFT(CONVERT(CHAR(19), @dt, 0), 11) 
    WHEN 'SHORTDATEANDTIME' THEN 
        REPLACE(REPLACE(CONVERT(CHAR(19), @dt, 0), 
            'AM', ' AM'), 'PM', ' PM') 
    WHEN 'UNIXTIMESTAMP' THEN 
        CAST(DATEDIFF(SECOND, '19700101', @dt) 
        AS VARCHAR(64)) 
    WHEN 'YYYYMMDD' THEN 
        CONVERT(CHAR(8), @dt, 112) 
    WHEN 'YYYY-MM-DD' THEN 
        CONVERT(CHAR(10), @dt, 23) 
    WHEN 'YYMMDD' THEN 
        CONVERT(VARCHAR(8), @dt, 12) 
    WHEN 'YY-MM-DD' THEN 
        STUFF(STUFF(CONVERT(VARCHAR(8), @dt, 12), 
        5, 0, '-'), 3, 0, '-') 
    WHEN 'MMDDYY' THEN 
        REPLACE(CONVERT(CHAR(8), @dt, 10), '-', SPACE(0)) 
    WHEN 'MM-DD-YY' THEN 
        CONVERT(CHAR(8), @dt, 10) 
    WHEN 'MM/DD/YY' THEN 
        CONVERT(CHAR(8), @dt, 1) 
    WHEN 'MM/DD/YYYY' THEN 
        CONVERT(CHAR(10), @dt, 101) 
    WHEN 'DDMMYY' THEN 
        REPLACE(CONVERT(CHAR(8), @dt, 3), '/', SPACE(0)) 
    WHEN 'DD-MM-YY' THEN 
        REPLACE(CONVERT(CHAR(8), @dt, 3), '/', '-') 
    WHEN 'DD/MM/YY' THEN 
        CONVERT(CHAR(8), @dt, 3) 
    WHEN 'DD/MM/YYYY' THEN 
        CONVERT(CHAR(10), @dt, 103) 
    WHEN 'HH:MM:SS 24' THEN 
        CONVERT(CHAR(8), @dt, 8) 
    WHEN 'HH:MM 24' THEN 
        LEFT(CONVERT(VARCHAR(8), @dt, 8), 5) 
    WHEN 'HH:MM:SS 12' THEN 
        LTRIM(RIGHT(CONVERT(VARCHAR(20), @dt, 22), 11)) 
    WHEN 'HH:MM 12' THEN 
        LTRIM(SUBSTRING(CONVERT( 
        VARCHAR(20), @dt, 22), 10, 5) 
        + RIGHT(CONVERT(VARCHAR(20), @dt, 22), 3)) 
    ELSE 
        'Invalid format specified' 
    END 
    RETURN @dtVC 
END

Open in new window

0
 
LVL 1

Author Comment

by:wellsuited
ID: 22964029
(year(datefield) & Month(datefield) & Day(datefield)) AS NewField
0
 
LVL 1

Expert Comment

by:_savage
ID: 22967275
This was 'figured out' well after I posted *two* suitable solutions.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question