0

I have date format returned as 05-Jan, 12-feb etc.. when i convert current date using date object in javascript . I did something like this

            var curr = new Date(),
            curr_year = curr.getFullYear(),
            curr_month = curr.getMonth(),
            curr_day = curr.getDay(),
            today = new Date(curr_year, curr_month, curr_day, 0, 0, 0, 0);
            console.log(today);

Here the today is returned as invalid date i needed the create a timestamp which should not include minutes secs and millisecs as zero for date comparison of month and date alone based on that i can categories .Is there way to dynamically create a date and compare those dates for given format. And when i try to convert my date string using date object it returns year as 2001. how can i compare dates based upon current year. For eg: in php i have used mktime to create a date dynamically from given date format and compare those results. Any suggestion would be helpful. Thanks.

4
  • 1
    Documentation for JS Date Commented Feb 4, 2013 at 11:18
  • @Westie That's a C# topic, this is a JavaScript question. Commented Feb 4, 2013 at 11:19
  • Damn! You're right - half asleep this morning! Commented Feb 4, 2013 at 11:22
  • So if you fix the typo the date isn't invalid anymore...if that's not what you want please make your question clearer. What are your expected input(s) and output? Commented Feb 4, 2013 at 11:23

4 Answers 4

1

You can leverage the native JS Date functionality to get human-readable date strings for time stamps.

var today = new Date();
console.log( today.toDateString() ); // Outputs "Mon Feb 04 2013"

Date comparison is also built in.

var yesterday = new Date();
yesterday.setDate( yesterday.getDate() - 1);
console.log( yesterday.toDateString() ); // Outputs "Sun Feb 03 2013"
console.log( yesterday < today ); //Outputs true

You can use the other built-in methods to fine-tune this comparison to be/not be sensitive to minutes/seconds, or to set all those to 0.

Sign up to request clarification or add additional context in comments.

5 Comments

sorry invalid date was a due to that error but the day returns as 01 bt today is 04 ..
@sathya Read the documentation. getDay Returns the day of the week (0-6) for the specified date according to local time. Today is Monday, the 1st day of the week, so it returned 1. If you want the date, use getDate.
i read the documentation and corrected those mistakes today = Date(curr_year, curr_month, curr_day, 0, 0, 0, 0); when print it still returns hours, minutes ,secs and milli secs of current time. if i needed to find birth day with my date format i.e 04-feb it will be equal at a sec only then it will return only greater or lesser.
@nbrroks srry my bad i missed new operator again .. Peace out
@sathya You can use the Date object's set methods to set the seconds etc. to 0, and wrap all those into a fn. you can call to normalize the date. Or, you can simply use string equality to compare the date string (which includes only the day and date, as shown in my answer above).
0

You said that you used mktime() in php, so what about this?

2 Comments

i ll try it and get back to you
i tried but it didnt give correct timestamp today = mktime(0,0,0, curr_day, curr_month+1, curr_year)
0

change to this :

        var curr = new Date(),
        curr_year = curr.getFullYear(),
        curr_month = curr.getMonth()+1,
        curr_day = curr.getDay(),
        today = curr_month+'/'+curr_day+'/'+curr_year;
        console.log(today);

(getMonth()+1 is because January is 0)

change the :

           today = curr_month+'/'+curr_day+'/'+curr_year;

to whatever format you like.

2 Comments

i wanted to create a timstamp regardless of min or secs
this is example without min/sec
0

I have found a way to convert the date into timestamp i have tried as @nbrooks implemented but .toDateString has built in date comparison which works for operator < and > but not for == operator to do that i have used Date.parse(); function to achieve it. Here it goes..

 var curr = new Date(),
 curr_year = curr.getFullYear(),
 curr_month = curr.getMonth(),
 curr_day = curr.getDate(),
 today = new Date(curr_year, curr_month, curr_day, 0,0,0,0);
 var dob = new Date('dob with month and date only'+curr_year);
 if(Date.parse(dob) == Date.parse(today)){
        //Birthdays....
        }

This method can be used to create a timestamp for dynamically created date.Thanks for your suggestions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.