javascript new date() at midnight

what is the simplest way to obtain an instance of new Date() but set the time at midnight?
bhomassAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd MummertCommented:
<SCRIPT LANGUAGE="JavaScript">
var t= new Date();
t.setHours(0);
t.setMinutes(0);
t.setSeconds(0);
t.setMilliseconds(0);
writeln("The date and time is " + t + "<BR>");
</SCRIPT>
0
bhomassAuthor Commented:
thanks, actually the end result I need is a bit more complicated and justifies more points too.

I want what ever javascript expression for this to fit inside eval(expression), and be able to apply date format to it so that the final result is string.

please help me with both the date formatting part and an expression that fits into eval().

for example

result = dateformatter.format('MM/dd/yyyy', eval(<the new date() expression for midnight>));
except the right syntax for the dateformatter
0
bhomassAuthor Commented:
furthermore, the I need the formatter to work for other than date format. that is if the result turns out to be double, string, boolean, integer. I need to apply the same process to all these input types.

the format should give all the clue as to what data type the result is in though.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Todd MummertCommented:
1) too much of a moving target

2) if all you want is 'MM/dd/yyyy', why do you care if it's midnight?

3) your last request is too vague, but it sounds like you want the function to infer type.


0
bhomassAuthor Commented:
the new date at midnight is one of the possible values I want to format. I want to include all the other possible values too.

what I am thinking of is one function

function getFormatedValue(format, expression){
    var result = eval(expression);
    return applyfomat(format, result);
}

does this clarify what I need? sorry about the changing target. I already added the points for the additional questions.
0
bhomassAuthor Commented:
the reason for the changing target is, as you can see, the solution you provided will not fit my need, even for one specific case. I need to be able to insert the expression inside eval().
0
Todd MummertCommented:

I think I understand what you want to do now in the general case, but I don't think it's possible w/o putting restrictions on the evaluated expression and it's type   and an understanding of how that type may be coerced into the format.

We'll see if anyone comes up with a solution.   It's an interesting question.
0
ZvonkoSystems architectCommented:
Here the expression:

var newDate = new Date((new Date()).getTime()-(new Date()).getTime()%86400000);

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bhomassAuthor Commented:
great idea.

I am going to close this thread. I think I got enough info to move on.
0
ZvonkoSystems architectCommented:
Before you move on rethink your grading.
If you want me to help you in your next questions then you have to do some extra work to correct your grading here. If you do not need my support then go on and leave it as is.

And what you have missed beside the "school grading" is also the cultural minimum to say: ThankYou ;-)
0
bhomassAuthor Commented:
i don't understand your objection. I commented "great". that's a favorable variation of "thank you" in most scripts. I can't see how you could feel offended with that?

the reason for the grading is that your answer did not cover the other aspect of the question, having to do with applying format. therefore, it is considered incomplete.

would you like to complete it or do you have some other argument why it is not an appropriate grade? I am open to ideas.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.