Adding class to item in array based on it's value - css

so I simply want to change the colour of the text of a list of arrays based on the word that's in the string.
Here's my array of combinations currently being outputted in the view with <%= #output %>:
[{"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}}, {"Mer rus sai ham"=>{:price=>12.1, :points=>32.2}}, {"Fer rus sai ham"=>{:price=>13.1, :points=>31.4}}, {"Mcl rus sai ham"=>{:price=>13.5, :points=>14.9}}]
CSS:
span.mer{color:red;}
span.lec{color:green;}
And then I am just calling it in my view using <%= #combo %>
The ouput should be the list of combinations but this time just the words are coloured correctly. This is currently just showing nothing, it's not throwing errors though.
EDIT
Added my full controller code in case it's needed:
# DEFINE VARIABLE AND TARGET
teams = team_price.keys
drivers = driver_price.keys
target = 13.5
# CREATE METHOD TO SUM BOTH PRICES AND POINTS FOR GIVEN COMBINATION
def add_up(combo, ht, hd)
t, d = combo
ht[t] + hd.values_at(*d).sum
end
# METHOD TO DOUBLE HIGHEST DRIVER POINTS
def add_dbl(combo, team_points, driver_points)
t, ds = combo
dmax = ds.max_by { |d| driver_points[d] }
driver_points[dmax] + add_up(combo, team_points, driver_points)
end
# ALL POSSIBLE COMBINATIONS OF TEAM AND DRIVERS
all_driver_combos = drivers.combination(3).to_a
all_combos = teams.product(all_driver_combos)
# SHOW ALL COMBOS WHERE SUM DOES NOT EXCEED TARGET
valid_combos = all_combos.select do |c|
add_up(c, team_price, driver_price) <= target
end
# SORT VALID COMBOS BY SUM OF POINTS FOR EACH ELEMENT
ordered = valid_combos.sort_by do |c|
-add_dbl(c, team_points, driver_points)
end
# SORT DRIVERS BY HIGHEST POINTS
ordered.each do |_t,ds|
ds.sort_by! { |d| -driver_points[d] }
end
# OUTPUT
output = ordered.map do |c|
{ c.join(" ")=>{ price: add_up(c, team_price, driver_price),
points: add_dbl(c, team_points, driver_points).round(2)} }
end
# CONVERT OUTPUT TO HASH
#output = output.reduce Hash.new, :merge
Table Edit
I would now like to place this combination output in a table as follows (whilst keeping the colour coding solution):
Team
Double
Drivers
Price
Points
Mer
lec
sai ham
13.1
53.4
Mer
rus
sai ham
12.1
32.2
Fer
rus
sai ham
13.1
31.4
In my view, I now have:
<table class="table table-condensed">
<thead>
<tr>
<th>Combo</th>
<th>Price</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% #output.each do |key,value| %>
<tr>
<td><%= raw(key.gsub(/Mer/, '<span class="mer">Mer</span>')) %></td>
<td><%= value[:price] %></td>
<td><%= value[:points] %></td>
</tr>
<% end %>
</tbody>
</table>
Which is currently outputting:
Combo Price Points
Mer lec sai ham 13.1 53.4
Mer rus sai ham 12.1 32.2
Fer rus sai ham 13.1 31.4
Mcl rus sai ham 13.5 14.9
However as shown in the table example above I would like it so that 'Combo' is split up into the correct columns. Like so:
Before:
Mer lec sai ham | 13.1 | 53.4
After:
Mer | lec | sai ham | 13.1 | 53.4
Price and points work fine, but it's just a case of splitting up the combo. Looking at the table I would like to create, the 'team' (e.g Mer, fer) is always going to be the first value in the array. 'Double' is always the second value in the array (lec in the above case) and then 'Driver' is just the remaining values (sai ham in the above case). Because the array is ordered in this way is there a way to pull each out using something like: .key[1] or somehow assign each key a new name? Thanks for the help!

It looks like you are using assignment (=) when you should be using comparison (==)?
#combo = #output.each do |p|
if p = 'Mer'
class_name = 'red'
elsif p = 'lec'
class_name = 'green'
else
class_name = 'black'
end
end
Instead of
#combo = #output.each do |p|
if p == 'Mer'
class_name = 'red'
elsif p == 'lec'
class_name = 'green'
else
class_name = 'black'
end
end
Also you are passing a hash as p and trying to compare it to a string. You don't need combo at all and should leave interface concerns out of your controller code. I would get rid of #combo. As far as stying things using CSS I try to keep it semantic. So I would name classes like:
span.mer{color:red;}
span.lec{color:green;}
And just leave black as the default.
Edit
Also if you can change your #output to just a simple hash of items with the string as the key and the hash of price and points as the value like this:
#output = {"Mer lec sai ham"=>{:price=>13.1, :points=>53.4}, "Mer rus sai ham"=>{:price=>12.1, :points=>32.2}, "Fer rus sai ham"=>{:price=>13.1, :points=>31.4}, "Mcl rus sai ham"=>{:price=>13.5, :points=>14.9}}
Possible view code(Edited to address request for table):
<table class="table table-condensed">
<thead>
<tr>
<th>Team</th>
<th>Double</th>
<th>Drivers</th>
<th>Price</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<% #output.each do |key,value| %>
<tr>
<% key = key.split(' ') %>
<% key = "<td>#{key[0]}</td><td>#{key[1]}</td><td>#{key[2]} #{key[3]}</td>" %>
<%= raw(key.gsub(/Mer/, '<span class="mer">Mer</span>')) %>
<td><%= value[:price] %></td>
<td><%= value[:points] %></td>
</tr>
<% end %>
</tbody>
</table>
This will wrap any word with a <span> tag that applies the color to that word based on the css defs. If "Lec" could appear as "Lec" or "lec" I would change the regexp in gsub to be case insensitive.

Multiple problems:
you are using asignment (=) instead of comparison (==)
you are comparing (if you change it) a Hash to a string, which will never be true
you are assigning a local variable (class_name) which does not have an effect on the output
it seems you want to highlight each word with the color, not just the full sentence
you have an array of hashes, where the key is a "sentence" (at least multiple words) which you want to highlight seperately and the value is a hash again.
Here is something to get you started (first step is to get code working, next step would be to clean it up. E.g. moving things to helpers or components or decorators).
def whatever_action_this_is
#output_with_colors = #output.map do |item|
item.transform_keys do |key|
words = key.split(' ')
highlighted = words.map do |word|
content_tag(:span, word, class: css_class(word))
end
highlighted.join(' ')
end
end
end
private
def css_class(word)
case word.downcase
when 'mer' then 'red'
when 'lec' then 'green'
else 'black'
end
end
And then in your view, assuming some ERB template
<ul>
<% #output_with_colors.each do |item|
<% item.each do |key, value|
<li>
<%= key.html_safe %>
<br/>
<%= value %>
</li>
<% end %>
<% end %>
</ul>

Related

How to extract title name and rating of a movie from IMDB database?

I'm very new to web scrapping in python. I want to extract the movie name, release year, and ratings from the IMDB database. This is the website for IMBD with 250 movies and ratings https://www.imdb.com/chart/moviemeter/?ref_=nv_mv_mpm.I use the module, BeautifulSoup, and request. Here is my code
movies = bs.find('tbody',class_='lister-list').find_all('tr')
When I tried to extract the movie name, rating & year, I got the same attribute error for all of them.
<td class="title column">
Glass Onion: une histoire à couteaux tirés
<span class="secondary info">(2022)</span>
<div class="velocity">1
<span class="secondary info">(
<span class="global-sprite telemeter up"></span>
1)</span>
<td class="ratingColumn imdbRating">
<strong title="7,3 based on 207 962 user ratings">7,3</strong>strong text
title = movies.find('td',class_='titleColumn').a.text
rating = movies.find('td',class_='ratingColumn imdbRating').strong.text
year = movies.find('td',class_='titleColumn').span.text.strip('()')
AttributeError Traceback (most recent call last)
<ipython-input-9-2363bafd916b> in <module>
----> 1 title = movies.find('td',class_='titleColumn').a.text
2 title
~\anaconda3\lib\site-packages\bs4\element.py in getattr(self, key)
2287 def getattr(self, key):
2288 """Raise a helpful exception to explain a common code fix."""
-> 2289 raise AttributeError(
2290 "ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
2291 )
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
Can someone help me to solve the problem? Thanks in advance!
To get the ResultSets as list, you can try the next example.
from bs4 import BeautifulSoup
import requests
import pandas as pd
data = []
res = requests.get("https://www.imdb.com/chart/moviemeter/?ref_=nv_mv_mpm.I")
#print(res)
soup = BeautifulSoup(res.content, "html.parser")
for card in soup.select('.chart.full-width tbody tr'):
data.append({
"title": card.select_one('.titleColumn a').get_text(strip=True),
"year": card.select_one('.titleColumn span').text,
'rating': card.select_one('td[class="ratingColumn imdbRating"]').get_text(strip=True)
})
df = pd.DataFrame(data)
print(df)
#df.to_csv('out.csv', index=False)
Output:
title year rating
0 Avatar: The Way of Water (2022) 7.9
1 Glass Onion (2022) 7.2
2 The Menu (2022) 7.3
3 White Noise (2022) 5.8
4 The Pale Blue Eye (2022) 6.7
.. ... ... ...
95 Zoolander (2001) 6.5
96 Once Upon a Time in Hollywood (2019) 7.6
97 The Lord of the Rings: The Fellowship of the Ring (2001) 8.8
98 New Year's Eve (2011) 5.6
99 Spider-Man: No Way Home (2021) 8.2
[100 rows x 3 columns]
Update: To extract data using find_all and find method.
from bs4 import BeautifulSoup
import requests
import pandas as pd
headers = {'User-Agent':'Mozilla/5.0'}
data = []
res = requests.get("https://www.imdb.com/chart/moviemeter/?ref_=nv_mv_mpm.I")
#print(res)
soup = BeautifulSoup(res.content, "html.parser")
for card in soup.table.tbody.find_all("tr"):
data.append({
"title": card.find("td",class_="titleColumn").a.get_text(strip=True),
"year": card.find("td",class_="titleColumn").span.get_text(strip=True),
'rating': card.find('td',class_="ratingColumn imdbRating").get_text(strip=True)
})
df = pd.DataFrame(data)
print(df)
AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
find_all returns an array, meaning that movies is an array. You need to iterate over the array with for movie in movies:
for movie in movies:
title = movie.find('td',class_='titleColumn').a.text
rating = movie.find('td',class_='ratingColumn imdbRating').strong.text
year = movie.find('td',class_='titleColumn').span.text.strip('()')

Deduction List - ASP Arrays

I want to display a breakdown of total investment deductions as an array list. The total amount of deductions is $-20.24 and my list breakdown is not adding up to that amount. I'm not too sure where I got it wrong. Please review my code and provide feedback. See total value being returned below:
Date Units Unit price Value
30/04/2018 -4.203 $ 1.99143 $ -8.37
30/04/2018 -0.366 $ 1.99454 $ -0.73
30/04/2018 -1.576 $ 3.54061 $ -5.58
30/04/2018 -0.138 $ 3.55072 $ -0.49
30/04/2018 -1.871 $ 2.49065 $ -4.66
30/04/2018 -0.164 $ 2.50000 $ -0.41
Total amount $ 16.98
<%
Dim objMemberClient, SwitchList
set objMemberClient = Server.createObject("MemberServiceProxy")
SwitchList= objMemberClient.GetInvestmentTransactionObjList(session("MemberId"),session("FundCode"), request.Querystring("date"), request.Querystring("date"), request.Querystring("description"))
%>
<h1>Investments</h1>
<div class="table-responsive">
<%if request.Querystring("description") = "Deduction" then %>
<TABLE class="table">
<%for i = LBound(SwitchList) to UBound(SwitchList)%>
<%if SwitchList(i).DeductionCode = getDesc(request.Querystring("subtype")) then%>
<tr>
<%if SwitchList(i).DeductionSign = true then%>
<td class="table_Header" width="200px">Investment sold</td>
<%exit for%>
<%end if%>
<%end if%>
<%Next%>
<td class="table_Header" width="125px">Date</td>
<td class="table_Header" width="125px">Units</td>
<td class="table_Header" width="125px">Unit price</td>
<td class="table_Header" width="125px">Value</td>
</tr>
<%for i = LBound(SwitchList) to UBound(SwitchList)%>
<%if SwitchList(i).DeductionCode = getDesc(request.Querystring("subtype")) then%>
<tr>
<td valign="top" class="border_Bottom"> <%=SwitchList(i).InvestmentOption.Name%></td>
<td valign="top" class="border_Bottom"><%=SwitchList(i).InvestmentDate%></td>
<td valign="top" class="border_Bottom"> <%=SwitchList(i).NumberUnits%></td>
<%if SwitchList(i).DeductionSign = true then %>
<%total = total + SwitchList(i).SwitchOutDollarValue%>
<%total = total * -1%>
<td valign="top" class="border_Bottom">$ <%=FormatNumber(SwitchList(i).SwitchOutDollarValue/Replace(SwitchList(i).NumberUnits,"-",""),5)%></td>
<td valign="top" class="border_Bottom">$ -<%=FormatNumber(SwitchList(i).SwitchOutDollarValue,2)%></td>
<%end if%>
</TABLE>
It would be easier to debug if we had something we could execute, but just guessing I think your problem is here:
<%total = total + SwitchList(i).SwitchOutDollarValue%>
<%total = total * -1%>
Total is a negative value, SwitchOutDollarValue I believe is a positive value, so comibining them in this way is not going to get the result you want.
As #SearchAndResQ mentioned, I would remove <%total = total * -1%> from inside the for loop and move it to just before you display the total.

Index Error: list out of range on html table web-scrape - Python

I appreciate this has been asked many times but I've been stuck here for quite a while.
I'm trying to take all the data from a table on a website and put it into a pandas dataframe.
I've written the code to do the web scraping but for some reason i'm getting the error whilst trying to write to my variable.
import requests
import requests
url = 'http://www.londonstockexchange.com/exchange/prices/stocks/summary/fundamentals.html?fourWayKey=GB00BCDBXK43GBGBXASX1'
page = requests.get(url).text
from bs4 import BeautifulSoup
soup = BeautifulSoup(page)
# print(soup.prettify())
all_tables = soup.find_all('table')
right_table = soup.find_all('table', {'class':'table_dati'})
tbl1 = right_table[0]
A = []
B = []
C = []
D = []
E = []
F = []
for row in tbl1.find_all('tr'):
cells = row.find_all('td')
A.append(cells[0].find(text = True))
B.append(cells[1].find(text = True))
C.append(cells[2].find(text = True))
D.append(cells[3].find(text = True))
E.append(cells[4].find(text = True))
F.append(cells[5].find(text = True))
Here's the error:
A.append(cells[0].find(text = True))
IndexError: list index out of range
Appreciate the help,
Thanks
Well, if you see the html code, your first iteration doesn't have td (is the thead), so when you are trying to get the first element, it doesn't exist, because cells are empty.
This is the first row:
<tr>
<th class="name">Income Statement</th>
<th>
31-May-13 <br>( £
m )
</th>
<th>
31-May-14 <br>( £
m )
</th>
<th>
31-May-15 <br>( £
m )
</th>
<th>
31-May-16 <br>( £
m )
</th>
<th>
31-May-17 <br>( £
m )
</th>
</tr>
You can surround with try, except, or select the tbody.
Based on your code, you can add to the find_all() a list of tags, and
then jump when the length of the cells list is less than 6, but for the future it's better to try to create lists dynamically, instead of everything being fixed.
for row in tbl1.find_all('tr'):
try:
cells = row.find_all(['td', 'th'])
if len(cells) < 6:
continue
A.append(cells[0].find(text = True).strip())
B.append(cells[1].find(text = True).strip())
C.append(cells[2].find(text = True).strip())
D.append(cells[3].find(text = True).strip())
E.append(cells[4].find(text = True).strip())
F.append(cells[5].find(text = True).strip())
except Exception as e:
print(e)
print(A)
The output is:
[
"Income Statement",
"Revenue",
"Operating Profit/(Loss)",
"Net Interest",
"Profit Before Tax",
"Profit After Tax",
"Profit After Tax",
"PROFIT FOR THE PERIOD",
"Minority Interests",
"Equity Holders of Parent Company",
"Earnings per Share - Basic",
"Earnings per Share - Diluted",
"Earnings per Share - Adjusted",
"Earnings per Share - Basic",
"Earnings per Share - Diluted",
"Earnings per Share - Adjusted",
"Dividend per Share"
]

Positionating and arrow direction in GraphViz

I'm trying to migrate some realy old documentation to our internal wiki using GraphViz.
I'm not used to the Dot language, and needs some help
See following example:
I have experiment a lot, but the best I have come up to so far is this:
digraph CentralPmr {
fontname="Helvetica";
shape=box;
node[shape=box];
graph [splines=ortho]
sg [label="TTD storage group for\nthe logged values"]
vc [label="Value catalogue"]
tc1 [label="Time catalogoue (1)"]
tc2 [label="Time catalogoue (2)"]
sv_ [shape=point,width=0.01,height=0.01];
sv [label=""]
ie [shape=none, label="Initiating event"]
c1 [shape=none, label="The set of values, defined\nby the value catalogue, which\nare freezed out of the TTD\nstorage group of the actual log."]
c2 [shape=none, label="Time catalogue defining\nat what time around the\ninitiating event values\nshould be collected."]
sgf [shape=record, label="{<f0> 1|2|3|4|..}|{ | | | | }"]
sg -> sv_ [penwidth=4, dir=none];
sv_ -> sv -> tc2 [penwidth=4]
sv -> sgf:f0 [penwidth=4]
{vc, tc1} -> sg
c1 -> sv [style=dashed, arrowhead="open"];
{rank=min; ie}
{rank=same; sg c1}
{rank=same; vc sgf}
{rank=max; rc2}
}
It don't have to be exactly the same as the source, but I want it to be understandable.
The problems is:
How do I place the text between "Value catalogue" and "Time catalogue (1)"?
[Edit] How do I force the arrow to "TTD storage group for PMR-freezed value" to go from the side, and not from the above? It is a virtualization of a memory area, and the arrow are pointing to a specific memory post. In other images, it can point to a other memory post in the memory area (eg. 2 , 3, 4..).
Is it possible to create a zigzag line from the "Initiating event"?
How do I place the legends in the bottom that explains the different types of lines?
[edit] How do I add the comments above, under and to the right of the "TTD storage group for PMR-freezed values"?
[Edit] How do I make the "TTD storage group for PMR-freezed value" wider?
This is on top of my first answer in a way that editing that one would create too much confusion. I have tried to take all your needs into consideration and it only works (I believe) if you give up the splines=ortho requirement. Pls refer to the comments below my first answer. Here we go:
digraph CentralPmr {
fontname="Helvetica";
shape=box;
node[shape=box];
// graph [splines=ortho]
sg [label="TTD storage group for\nthe logged values", width = 2.5]
sv[ label="", width = 2]
ie [ shape=none, label="Initiating event", fontsize = 18 ]
c1 [ shape=none, label="The set of values, defined\nby the value catalogue, which\nare freezed out of the TTD\nstorage group of the actual log." ]
sgf[shape=box, margin=0, label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
<TR>
<TD BORDER="0" COLSPAN="2">TTD storage group for<BR/>PMR freezed values</TD>
</TR>
<TR>
<TD PORT="f1">1</TD>
<TD BORDER="0" ROWSPAN="6">The set of<BR/>values is<BR/>stored in<BR/>the TTD<BR/>storage<BR/>group</TD>
</TR>
<TR>
<TD>2</TD>
</TR>
<TR>
<TD>3</TD>
</TR>
<TR>
<TD>4</TD>
</TR>
<TR>
<TD>-</TD>
</TR>
<TR>
<TD>-</TD>
</TR>
<TR>
<TD BORDER="0" COLSPAN="2">Up to nine freezing areas<BR/>for defined central PMR</TD>
</TR>
</TABLE>>];
TTD [shape=none, margin=0, label=<
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="12">
<TR>
<TD PORT="f1">Value catalogue</TD>
</TR>
<TR>
<TD BORDER="0"></TD>
</TR>
<TR>
<TD PORT="f2">Time catalogue (1)</TD>
</TR>
<TR>
<TD BORDER="0">Time catalogue defining<BR/>at what time around the<BR/>initiating event values<BR/>should be collected</TD>
</TR>
<TR>
<TD PORT="f3">Time catalogue (2)</TD>
</TR>
</TABLE>>];
connector_1[ shape = point height = 0 width = 0 margin = 0 ]
ie -> connector_1[ style = dotted, arrowhead = none ];
{ rank = same; connector_1 c1 }
connector_1 -> c1[ style = invis, minlen = 4 ];
c1 -> sv[ style = dashed, arrowhead = open ];
connector_2[ shape = point height = 0 width = 0 margin = 0 ]
connector_1 -> connector_2[ style = dotted ];
{ rank = same; sg connector_2 sv }
sg -> connector_2[ minlen = 3, penwidth = 4, arrowhead = none ];
connector_2 -> sv[ minlen = 3, penwidth = 4 ];
sg:sw -> TTD:f1:nw[ weight = 5 ];
sg:w -> TTD:f2:w;
sv:sw -> TTD:f3:e[ penwidth = 4 ];
sv:sw -> sgf:f1:w[ penwidth = 4 ];
node[ shape = plaintext ];
leg2[ label = "Data flow" ];
leg4[ label = "Reference" ];
leg6[ label = "Comment" ];
node [ shape = point height = 0 width = 0 margin = 0 ];
leg1 leg3 leg5
TTD:sw -> leg1[ style = invis ];
{ rank = same; leg1 leg2 leg3 leg4 leg5 leg6 }
edge[ minlen = 2 ];
leg1 -> leg2[ penwidth = 4 ];
leg3 -> leg4[ style = dotted ];
leg5 -> leg6[ style = dashed, arrowhead = open ];
}
yields
Not sure whether I understand completely what you want but below my take on it. This is just a first attempt, much more fine-tunig can be done. I would probably use HTML-like nodes where text and "box" need to be closer, in particular for that "TTD Storage Group for PMR freezed values" in the original graph.
My answers to your questions would be:
How do I place the text between "Value catalogue" and "Time catalogue (1)"?
--- See below. I have put it between the two time catalogues as in the original graph but easy to move around.
How do I force the arrow to the record go from the side, and not from the above?
--- See below. You could also use rankdir = LR; to change the orientation if that is your question.
Is it possible to create a zigzag line from the "Initiating event"?
--- There are ways, but a lot of effort (like creating a custom shape). Nothing "out of the box", to the best of my knowledge.
How do I place the legends in the bottom?
I don't really understand, but in general, the answer would be HTML-like labels when we talk about nodes.
Her is what I have done:
digraph CentralPmr
{
fontname="Helvetica";
shape=box;
node[shape=box];
graph [splines=ortho]
sg [label="TTD storage group for\nthe logged values"]
vc [label="Value catalogue"]
tc1 [label="Time catalogoue (1)"]
tc2 [label="Time catalogoue (2)"]
sv_ [shape=point,width=0.01,height=0.01];
sv [label="", width = 2]
ie [shape=none, label="Initiating event"]
c1 [shape=none, label="The set of values, defined\nby the value catalogue, which\nare freezed out of the TTD\nstorage group of the actual log."]
c2 [shape=none, label="Time catalogue defining\nat what time around the\ninitiating event values\nshould be collected."]
sgf [shape=record, label="{<f0> 1|2|3|4|..}|{ | | | | }"]
connector_1[ shape = point height = 0 width = 0 margin = 0 ]
ie -> connector_1[ style = dotted, arrowhead = none ];
{ rank = same; connector_1 c1 }
connector_1 -> c1[ style = invis ];
c1 -> sv[ style = dashed, arrowhead = open ];
connector_2[ shape = point height = 0 width = 0 margin = 0 ]
connector_1 -> connector_2[ style = dotted ];
{ rank = same; sg connector_2 sv }
sg -> connector_2[ minlen = 3, penwidth = 4, arrowhead = none ];
connector_2 -> sv[ minlen = 3, penwidth = 4 ];
vc -> tc1 -> c2 -> tc2[ style = invis, weight = 10 ];
sg -> vc;
sg -> tc1;
sv -> tc2[ penwidth = 4 ];
sv -> sgf;
}
yields

ASP formatting date

Hello there I'm trying to get a date in ASP to show up in a particular format (yyyymmdd). This is what I've tried so far but no luck. Any help is appreciated. Thanks
<tr>
<td><b>Call Date</b></td>
<% for i = -6 to 0 %>
<td align=center>
X
</td>
<% Next %>
</tr>
You can make use of the following functions:
Year(Now) '' Year in 4 digits
Month(Now) '' Month without leading zero
Day(Now) '' Day of the month without leading zero
DateAdd("d", <numofdays>, Now) '' add a number of days to your date
Read more about these (and other date functions) functions here.
If you need to add a leading zero:
function addLeadingZero(value)
addLeadingZero = value
if value < 10 then
addLeadingZero = "0" & value
end if
end function
An example of your case would be:
Dim today, myDate
today = Now
for i = -6 to 0
myDate = DateAdd("d", i, today)
response.write "X"
next
Sorry to dig this up, but it might be of help to some people. Rather than the "If<10 then add leading zero" logic, I often use the right command and always add a leading zero...
response.write "X"
..This way, you don't need a separate function, and it can be done on one line. I can't speak for the efficiency of it, but it seems logical.
ASP gets the date from the OS not from the Database, a common error, but it is solved by use:
<%
' Date dd/mm/yyyy
Session.lcid=2057 '= UK English
%>
I hope it helps people.
You can try. 100% tested!
<%
mm = Month(now())
dd = Day(now())
yy = Year(now())
IF len(mm) = 1 THEN
mm = "0" & mm
END IF
IF len(dd) = 1 THEN
dd = "0" & dd
END IF
response.write(yy & "/" & mm & "/" & dd)
%>
<%= DatePart("yyyy", Now) & "/" & DatePart("m", Now) & "/" & DatePart("d", Now) %>
Also refer
http://www.w3schools.com/vbscript/vbscript_ref_functions.asp
http://www.mikesdotnetting.com/Article/22/Date-formatting-in-VBScript
Thanks
Deepu
Mid(date(), 7,4) & "-" & Mid(date(), 4,2) & "-" & Left(date(), 2)

Resources