Solved

Extract date

Posted on 2012-04-12
5
173 Views
Last Modified: 2012-04-17
I'm trying to use the code below to extract the date with out the time.
The result I get is 1st of April  with a time stamp still.

<script type="text/javascript">
 <!--
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
document.getElementsByName('SubscriberDate')[0].value = ("0" +
curr_month).slice(-2) + "/" + curr_year;//-->
</script>

Open in new window

0
Comment
Question by:Isaac
5 Comments
 
LVL 7

Expert Comment

by:dimmergeek
ID: 37839934
Good afternoon.

This code displays 04/2012 in IE, Chrome and FF.

    <script type="text/javascript">
        var d = new Date();
        var m = d.getMonth() + 1;
        var y = d.getFullYear();
        if (m < 10) {
            document.write('0');
        }
        document.write(m + '/' + y);
    </script>

I ran your code and got an error at the .slice.  All I saw on my screen was '04'.
0
 
LVL 5

Author Comment

by:Isaac
ID: 37839972
How do I get it to display the full date?

Thanks!
0
 
LVL 7

Assisted Solution

by:dimmergeek
dimmergeek earned 100 total points
ID: 37840083
<script type="text/javascript">
        var d = new Date();
        var m = d.getMonth() + 1;
        var dt = d.getDay();
        var y = d.getFullYear();
        if (m < 10) {
            document.write('0');
        }
        document.write(m + '/');
        if (dt < 10)
        {
            document.write('0');
        }
        document.write(dt + '/' + y);

Full date with zeros ahead of month and day numbers when they are less than 10.
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 400 total points
ID: 37841330
Try this jsfiddle:

var date = new Date();

$("#SubscriberDate").text(date.toString("MM/YYYY"));

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 37842195
If you want to learn more about dates in Javascript, you might be interested in this article:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_484-Can-I-have-a-date.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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