0

So I apologize in advance, I am very new to Javascript. I'm in the middle of creating a feed but I'm stuck. The output for firstday is shown as "Mon Feb 03 2014 13:23:03 GMT-0500 (Eastern Standard Time)" and I would just like to read "February 3" or "Feburary 3rd". Please help!!! Here's my code

<script type="text/javascript">   
    Date.prototype.getWeek = function() {
        var onejan = new Date(this.getFullYear(),0,1);
        return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
    } 
    function getMonday(d) {
        var day = d.getDay(),
        diff = d.getDate() - day + (day == 0 ? -6 : 1)+14; 
        return new Date(d.setDate(diff));
    }

    var firstday = getMonday(new Date());               
    var weekNumber = (new Date()).getWeek()+3;

    // Array of day names
    var dayNames = "Week ";
    var now = new Date();
    document.write("<h2 align='center' style='margin-bottom:0;'>");
    document.write(dayNames + weekNumber);
    document.write("</h2>");
    document.write("<p align='center' style='margin-top:5px;'><strong>Deploying the week of ");
    document.write(firstday);
    document.write("</strong></p>");
</script>
Reger
  • 474
  • 4
  • 17
devb215
  • 11
  • 1
  • You could try and do this yourself but, I would recommend Momentjs (http://momentjs.com/) for the job. Dates in JS are quirky even across browsers and browser versions... – xspydr Jan 23 '14 at 18:50
  • Jason you are the MAN!!! I read about Moment before but I didn't think I needed it. Thanks a million!! – devb215 Jan 23 '14 at 19:26

4 Answers4

0
    function formatDate(theDate) {

        var month = theDate.getMonth()+1;
        var year = theDate.getYear();
        var day = theDate.getDate();
        var zero = "0";
        if(day<10) day = zero.concat(day);
        if(month<10) month = zero.concat(month);
        if(year<1000) year += 1900;

        t = (String(year) + '-' + String(month) + '-' + String(day));
        return t;
    }

    function stdDate(theDate) {

        var month = theDate.getMonth()+1;
        var year = theDate.getYear();
        var day = theDate.getDate();
        var zero = "0";
        if(day<10) day = zero.concat(day);
        if(month<10) month = zero.concat(month);
        if(year<1000) year += 1900;

        t = (String(day) + '.' + String(month) + '.' + String(year));
        return t;
    }
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
0

We can simply format a string using some methods to create our own formatted date:

var months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];

var date = new Date()
var month = date.getMonth(); // Returns the Month as a number 
var day = date.getDate(); // Returns the Day of the Month as a number 

var formattedDate = months[month]+" "+day; // January 23
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

.toLocaleString() function will solve your problem.

var date=new Date();
var arr=date.toLocaleString().split(' ');
document.write(arr[1]+" "+arr[0]);

see the fiddle here

EDIT

For adding text like 23rd, 21st, 15th, you can write a commonFunction like

function getSuffix(num) {
    var stringAppend = "";
    switch (num % 10) {
        case 1:
            stringAppend = "st";
            break;
        case 2:
            stringAppend = "nd";
            break;
        case 3:
            stringAppend = "rd";
            break;
                default:
            stringAppend = "th";
            break;
    }
    return num + stringAppend;
}
Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35
0

I really recommend you to stay away from manual calculations and use a very good and tested library for parsing, validating, manipulating and formatting dates, called Moment.js.

That way you can safely rely on it for the calculations and simplify your code to this:

// Get last monday.
var lastMonday = moment().startOf('isoweek');
// Add two weeks to last monday.
var nextSecondMonday = lastMonday.add('weeks', 2);
// Format the next second monday for presentation.
var firstDay = nextSecondMonday.format('MMMM Do');
// Get next second monday week number.
var weekNumber = nextSecondMonday.isoWeek();

document.write("<h2 align='center' style='margin-bottom:0;'>");
document.write('Week ' + weekNumber);
document.write("</h2>");
document.write("<p align='center' style='margin-top:5px;'><strong>Deploying the week of ");
document.write(firstDay);
document.write("</strong></p>");

This code assumes that you're doing some operation starting now and closing in two week's monday (like opening a new development sprint, for example) and you need to get those values for the user to know when it'll be deployed if it starts now. Please be aware that next week, it will show another date because it's always adding 2 weeks. So for example, if the result is Feb 3rd, next week it'll show Feb 10th and won't keep Feb 3rd until Feb 3rd. I'm not sure if this is what you need because i looked quickly at your code and you're not putting comments on calculations (which you should do for later). If you need to maintain the date until Feb 3rd, then it shouldn't be too difficult to change this code doing some math and using Moment.js.

Alejandro García Iglesias
  • 16,222
  • 11
  • 51
  • 64