Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-06-2011, 03:50 PM   PM User | #1
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
First Post - Using Date Object

This is my first post... and a quick question that is mostly likely easy for most of you!!

I am pulling in some data from an MySQL database and generating a graph using RGraph and some HTML.

The date pulled in is shown as 2011-01-01 for example.

I would like to simply format the date and turn into Jan, 01, 11 or similar.

Any recommendations on using Date Object to format that in to the date text?
sryder is offline   Reply With Quote
Old 10-06-2011, 04:40 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You do not need the javascript date object. Simply split your date at the - delimiter and then manipulate it as you will.


Code:
<script type = "text/javascript">
var months=["January","February","March","April", "May", "June","July","August","September", "October", "November","December"];
var dt = "2011-01-01";
var d = dt.split("-");
var m = Number(d[1])-1;  // months array is index 0-11
var nd = months[m] + " " + d[2] + " " + d[0];  // or whatever arrangement you like
alert (nd);
</script>
At least once per year, some group of scientists will become very excited and announce that whatever they announced last year about global warming is wrong.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-06-2011 at 04:42 PM..
Philip M is offline   Reply With Quote
Old 10-06-2011, 05:03 PM   PM User | #3
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
thanks very much for the reply!

Just a follow up question though... the date is pulled in dynamically and not simply static - so how would i do it that way?
sryder is offline   Reply With Quote
Old 10-06-2011, 06:36 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
var dt = "The Date Which Is Pulled In Dynamically From Your Database";
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 10-06-2011, 07:10 PM   PM User | #5
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
This is how the date is pulled:

$labels = array();
$data = array();

while ($row = mysql_fetch_assoc($result)) {
$labels[] = $row["WeekEndDate"];
$data[] = $row["CustomerPay"];
}

// Now you can aggregate all the data into one string
$data_string = "[" . join(", ", $data) . "]";
$labels_string = "['" . join("', '", $labels) . "']";
} else {

So would it be var dt = " $labels"; ?

Thanks,
Shawn
sryder is offline   Reply With Quote
Old 10-06-2011, 08:30 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No, because $labels is an array, presumably of many dates.

But if you are supplying the date from PHP, why not simply reformat it in the PHP?

Or, for that matter, reformat it in the MySQL query?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-06-2011, 09:57 PM   PM User | #7
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
Simple answer... because I don't know how to do that. :-(
sryder is offline   Reply With Quote
Old 10-06-2011, 10:09 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Ahhh...so show me the SQL query you are using and I'll show you how.

Then you won't have to change anything else.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-07-2011, 12:02 PM   PM User | #9
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Could be
Code:
<script type="text/javascript">
var dt=[];
<?php
for($data as $key=>$datum){
echo 'dt[".$key."]="'.$datum.'";';
}
?>
</script>
Make sure the php variable is already defined when you write that.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 10-07-2011, 02:19 PM   PM User | #10
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
This is where I pull in the data - should i put the variable in here?

Code:
$connection = mysql_connect($hostname, $username, $password) OR die('Could not connect to MySQL: ' . mysql_error());
    mysql_select_db($database);
    
  $result = mysql_query("select CustomerPay, WeekEndDate from " .$x->QueryFrom . ' ' . $x->QueryWhere . ' ' . " order by WeekEndDate ASC ");
    if ($result) {
sryder is offline   Reply With Quote
Old 10-07-2011, 05:26 PM   PM User | #11
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
I am not exactly sure where to place the code yet... will have to test it out.

Quote:
Originally Posted by Kor View Post
Could be
Code:
<script type="text/javascript">
var dt=[];
<?php
for($data as $key=>$datum){
echo 'dt[".$key."]="'.$datum.'";';
}
?>
</script>
Make sure the php variable is already defined when you write that.
sryder is offline   Reply With Quote
Old 10-07-2011, 07:40 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I still say you should do this in the MySQL query.

Code:
$sql = "select CustomerPay, DATE_FORMAT(WeekEndDate,'%b %d, %y') AS PrettyWeekEndDate "
     . " from " .$x->QueryFrom . " "
     . $x->QueryWhere 
     . " order by WeekEndDate ASC "
$result = mysql_query( $sql );
if ($result) { ...
See here for other formatting options:
http://dev.mysql.com/doc/refman/5.1/...on_date-format
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-11-2011, 03:47 PM   PM User | #13
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
And then I use the "PrettyWeekEndDate" in the code down later, right?

Quote:
Originally Posted by Old Pedant View Post
I still say you should do this in the MySQL query.

Code:
$sql = "select CustomerPay, DATE_FORMAT(WeekEndDate,'%b %d, %y') AS PrettyWeekEndDate "
     . " from " .$x->QueryFrom . " "
     . $x->QueryWhere 
     . " order by WeekEndDate ASC "
$result = mysql_query( $sql );
if ($result) { ...
See here for other formatting options:
http://dev.mysql.com/doc/refman/5.1/...on_date-format
sryder is offline   Reply With Quote
Old 10-11-2011, 05:29 PM   PM User | #14
sryder
New to the CF scene

 
Join Date: Oct 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
sryder is an unknown quantity at this point
I got it to work!! Thanks everybody!!
sryder is offline   Reply With Quote
Reply

Bookmarks

Tags
date, mysql, object

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:55 AM.


Advertisement
Log in to turn off these ads.