Solved

Extract date

Posted on 2012-04-12
5
170 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
Comment Utility
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
Comment Utility
How do I get it to display the full date?

Thanks!
0
 
LVL 7

Assisted Solution

by:dimmergeek
dimmergeek earned 100 total points
Comment Utility
<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
Comment Utility
Try this jsfiddle:

var date = new Date();

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

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now