简体   繁体   中英

Changing the time and date format

I would like to learn how can I run and see the display in this code :

var now = new Date();

now.format("m/dd/yy");
// Returns, e.g., 6/09/07

// Can also be used as a standalone function
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
// Saturday, June 9th, 2007, 5:46:21 PM

Thanks.

Be sure that you download the plugin and include it somewhere either in the head of your page, or beneath the body like this

<script type='text/javascript' src='pathToFile.js'></script>

Next, if you're using a modern browser, you should have a debugger console in which you can test code. If you're using Firefox, take a look at firebug , and if you're using Chrome, you already have one, just hit CTRL+SHIFT+J to pull up the console. Once inside of the console, you can enter the commands as you entered them above and see the results. I hope this helps!

Back in the old days...

To view the outcome of your code, you'd traditionally need to make an HTML page and stick that into the <script type="javascript">...</script> section of your code.

Nowadays!

Nowadays, you can do it right online! Try plugging your code into this: http://writecodeonline.com/javascript/

No output?

you can use document.write( now ); to display your variable now to the screen.

Still no output?

I believe that dateFormat and format will give you errors as JavaScript doesn't know what this is (unless you wrote these methods in your code). See this StackOverflow question for some resources with formatting Javascript Dates.

如果您正在寻找一个可以简化日期处理的库,建议使用Datejs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM