0

In Python I have this code:

now =  datetime.now().isoformat()
if "." not in now:
  now = now + ".000000"

Can I achieve the same result in Javascript?

The resulting datetime should match this mask %Y-%m-%dT%H:%M:%S.%f because the datetime will be saved into the database and then I need to retrieve it from Python code with exactly this mask.

5
  • 1
    It would have helped if you had shown us the expected result... Yet, you might be interested in: blog.stevenlevithan.com/archives/date-time-format Commented Jun 25, 2012 at 10:55
  • @JMax "ISO format" is well known, and well defined. Commented Jun 25, 2012 at 10:56
  • I'm curious as to why you have the if "." not in now check? Is there some scenario where the iso format doesn't return .? Commented Jun 25, 2012 at 11:01
  • @BurhanKhalid Good point. There was such a case before, but it was not retrieved from datetime.now() and the .something part was missing often. Maybe I could omit it now (I'm not sure). Commented Jun 25, 2012 at 11:06
  • possible duplicate of How do I output an ISO-8601 formatted string in Javascript? Commented Jun 25, 2012 at 15:00

2 Answers 2

3

Did you look at Date.toISOString() - the standard function for generating ISO 8601 dates?

Here's a Chrome console test:

> (new Date()).toISOString()
"2012-06-25T10:55:19.833Z"

Note that the link above includes a shim which adds support for this function to browsers that don't already have it.

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

5 Comments

@InternalServerError if you look at the link I included the MDN site includes a shim for older browsers.
Thanks for answer, actually I use it in Firefox extension, so the other solution suits better to my need.
@xralf you should use the standard function, and shim it when not available.
@Alnitak But I don't have the Z letter in the end. Python omits the Z letter.
so just do .substring(-1) on it then - the Z suffix is part of ISO 8601 format.
1

Last example on page: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Global_Objects:Date

/* use a function for the exact format desired... */
function ISODateString(d){
 function pad(n){return n<10 ? '0'+n : n}
 return d.getUTCFullYear()+'-'
  + pad(d.getUTCMonth()+1)+'-'
  + pad(d.getUTCDate())+'T'
  + pad(d.getUTCHours())+':'
  + pad(d.getUTCMinutes())+':'
  + pad(d.getUTCSeconds())+'Z'}

var d = new Date();
console.log(ISODateString(d)); // prints something like 2009-09-28T19:03:12Z

3 Comments

And if you prefer jquery...there u go plugins.jquery.com/project/jquery-dateFormat
Looks good, I removed only the Z letter and can I get microseconds too? In my mask it's .%f part.
Or I will concatenate it with .000000 string, to have a correct format.

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.