skip to Main Content

Right now i am using this function

const setDuration = () => {
        const currentDate = new Date();
        const newDate = new Date(currentDate.getTime());
        
        const year = newDate.getUTCFullYear();
        const month = String(newDate.getUTCMonth() + 1).padStart(2, '0');  
        const day = String(newDate.getUTCDate()).padStart(2, '0');
      
        const hours = String(newDate.getUTCHours()).padStart(2, '0');
        const minutes = String(newDate.getUTCMinutes()).padStart(2, '0');
        const seconds = String(newDate.getUTCSeconds()).padStart(2, '0');
        const milliseconds = String(newDate.getUTCMilliseconds()).padEnd(7, '0');

        const timeZoneOffset = newDate.getTimezoneOffset();
        const timeZoneOffsetHours = Math.abs(Math.floor(timeZoneOffset / 60));
        const timeZoneOffsetMinutes = Math.abs(timeZoneOffset % 60);
        const timeZoneOffsetSign = timeZoneOffset < 0 ? '-' : '+';
        return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}${timeZoneOffsetSign}${String(timeZoneOffsetHours).padStart(2, '0')}:${String(timeZoneOffsetMinutes).padStart(2, '0')}`;
    }

I am Expecting this format => 2023-09-14T10:12:03.7380000+00:00

3

Answers


  1. Yes, in JavaScript you can format a date into ISO 8601 format using the toISOString method available on Date objects.

    For your specific use you could use it like this:

    const setDuration = () => {
        const date = new Date();
        const timeZoneOffset = date.getTimezoneOffset();
        const timeZoneOffsetHours = String(Math.floor(Math.abs(timeZoneOffset) / 60)).padStart(2, '0');
        const timeZoneOffsetMinutes = String(Math.abs(timeZoneOffset) % 60).padStart(2, '0');
        const timeZoneOffsetSign = timeZoneOffset < 0 ? '+' : '-';
        
        const isoString = date.toISOString();
        return `${isoString.slice(0, -1)}${timeZoneOffsetSign}${timeZoneOffsetHours}:${timeZoneOffsetMinutes}`;
    };
    
    Login or Signup to reply.
  2. Is it 2023, every language has refactored its date API (see java evolution from Date, to Calendar, to Instant/DateTime), or wants to.
    Also it seems you want nanosecond (micro?), bu JS do not offer such possibility without the use of libraries. (you did not specify if browser or node runtime)

    It is js, so there are plentiful of large use libraries. So the case of not using libraries because you do not want to is wrong.

    Why not use a library (luxon, momen) that is tested to work in plenty of scenarios ? (like yours)

    Login or Signup to reply.
  3. I think this will solve your query

    class CustomDate {
      constructor(date) {
        this.date = date;
      }
    
      // Custom method to format the date as a string
      format() {
        const year = this.date.getUTCFullYear();
        const month = String(this.date.getUTCMonth() + 1).padStart(2, '0');
        const day = String(this.date.getUTCDate()).padStart(2, '0');
        const hours = String(this.date.getUTCHours()).padStart(2, '0');
        const minutes = String(this.date.getUTCMinutes()).padStart(2, '0');
        const seconds = String(this.date.getUTCSeconds()).padStart(2, '0');
        const milliseconds = String(this.date.getUTCMilliseconds()).padStart(3, '0');
    
        const timeZoneOffset = -this.date.getTimezoneOffset();
        const timeZoneOffsetHours = Math.floor(timeZoneOffset / 60);
        const timeZoneOffsetMinutes = timeZoneOffset % 60;
    
        const timeZoneOffsetSign = timeZoneOffset >= 0 ? '+' : '-';
        const timeZoneOffsetHoursStr = String(Math.abs(timeZoneOffsetHours)).padStart(2, '0');
        const timeZoneOffsetMinutesStr = String(Math.abs(timeZoneOffsetMinutes)).padStart(2, '0');
    
        return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}${timeZoneOffsetSign}${timeZoneOffsetHoursStr}:${timeZoneOffsetMinutesStr}`;
      }
    }
    
    const currentDate = new Date();
    const customDate = new CustomDate(currentDate);
    const formattedDate = customDate.format();
    
    console.log(formattedDate); // Output: "2023-09-14T10:12:03.7380000+00:00"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search