I need to use the Google Calendar service for editing single instances of an event series. The service returns datetime values in RFC 3339 format (like 2022-11-03T21:30:41.043Z), which is hard to process within Google apps script. Is there an easy way feeding it into Utilities.formatDate to convert it (ie yyyy-MM-dd HH:mm)?
Vice versa is plainly done with .toISOString().
You can convert an ISO8601 format date string into a JavaScript Date object directly with the Date constructor. To convert the Date object into a formatted date string, use Utilities.formatDate() with your preferred timezone, like this:
function test() {
const dateISO8601 = '2022-11-03T21:30:41.043Z';
const timezone = 'PST';
const dateString = toDateString_(dateISO8601, timezone);
console.log(dateString);
}
function toDateString_(dateISO8601, timezone = 'GMT') {
const date = new Date(dateISO8601);
return Utilities.formatDate(date, timezone, 'yyyy-MM-dd HH:mm');
}
Related
I have called an API and then get a date which is string format like '15/07/21-23:59:59'. But I want to convert this string into the actual date format like this:
**15/07/21** OR **2009-06-01T10:00:00.000**.
so how can I achieve this?
It's strange to see a response returning a formatted date expression. By the way, your task would be easily done with momentjs. Here is my snippet:
// since your date format is not a standard one, you would have to pass an
// instruction of your date format as a second parameter to the moment constructor
const momentDate = moment('15/07/21-23:59:59', 'DD/MM/YY-HH:mm:ss');
momentDate.format('DD/MM/YYYY'); // => "15/07/2021"
momentDate.format(`yyyy-MM-dd'T'HH:mm:ss.SSSZ`); // => "2021-07-Th'T'23:59:59.000+07:00"
you can pass this string to a Date object as follow:
var date = new Date(YOUR STRING);
or you can use Date.parse() method if it does not work:
Date.parse('04 Dec 1995 00:12:00 GMT');
Using Luxon JS, I've been trying to format datetime to output in a certain format, using the native toISO function:
This is what I get:
"2018-08-25T09:00:40.000-04:00"
And this is what I want:
"2018-08-25T13:00:40.000Z"
I know that they are both equivalent in terms of unix time and mean the same thing except in a different format, I just want to be able to out the second string rather than the first. I looked through the Luxon docs but was unable to find any arguments/options that would give me what I need.
As other already stated in the comments, you can use 2 approaches:
Convert Luxon DateTime to UTC using toUTC:
"Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.
Use toISOString() method of JS Date.
You can use toJSDate() to get the Date object from a luxon DateTime:
Returns a JavaScript Date equivalent to this DateTime.
Examples:
const DateTime = luxon.DateTime;
const dt = DateTime.now();
console.log(dt.toISO())
console.log(dt.toUTC().toISO())
console.log(dt.toJSDate().toISOString())
console.log(new Date().toISOString())
<script src="https://cdn.jsdelivr.net/npm/luxon#1.26.0/build/global/luxon.js"></script>
From documentation I saw that in the method .fromISO of DateTime you can add an option object after the string of ISO date ("2018-08-25T09:00:40.000-04:00" in your example). In this object specify zone: utc like that:
const DateTime = luxon.DateTime;
const stringDate = "2018-08-25T09:00:40.000-04:00";
const dt = DateTime.fromISO(stringDate, {zone: 'utc'});
console.log('This is your date format', dt.toISO())
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.26.0/luxon.min.js"></script>
I am building an online store, most of my customers (basically all) are located in a given timezone, but my infrastructure is located in other timezone (we can assume it's UTC). I have the option for my clients to select a date for their orders, the problem is that my date component represents dates like this "YYYY-MM-DD". In am using the Date constructor like this:
let dateString = "2019-06-03"
let date = new Date(dateString)
console.log(date) //This will print the local time zone representation of my dateString
console.log(date.toISOString()) //This will print the utc equivalent of my dateString
The problem with this is that I want the UTC representation to be calculated from the local timezone, not the other way around. Let's suppose in am located in GMT-5, when I say let date = new Date("2019-06-06") I want to see "2019-06-03T00:00:00.000 GMT-5" , and the ISOString should be "2019-06-03T05:00:00.000Z". How can I do this ?
What you are trying to achieve can be done by appending the string T00:00:00 to the dateString before passing it to the Date() constructor.
But a word of caution, manipulating the timezone/offsets manually like this might result in incorrect data being presented.
If you are storing and retrieving all the order timestamps in UTC only, it will avoid timezone related issues and you might not need to process the timestamps like this.
let dateString = "2019-06-03"
let date = new Date(dateString + "T00:00:00")
console.log(date) //This will print the local time zone representation of my dateString
console.log(date.toISOString()) //This will print the utc equivalent of my dateString
When the date is passed from my c# to JavaScript it returns the date time as {4/3/2020 12:00:00 AM}
but in JavaScript it is shown as 1585852200000.
What is the format that is being used? And how can i convert it back?
You need to convert the Unix timestamp to DateTime format,
var localDate = new Date(1585852200000).toLocaleDateString("en-US")
console.log(localDate); // only local date
var localTime = new Date(1585852200000).toLocaleTimeString("en-US")
console.log(localTime) // only local time
// local datetime
console.log(new Date(1585852200000).toLocaleString());
1585852200000 is epoch date.
you can convert it as
var date = new Date(1585852200000)
console.log(new Date(1585852200000));
As an alternative from Shivaji's answer:
When you are passing the date through to JS you could cast it as a string with DateTime.ToString("dd/MM/yyyy") seen here on MSDN.
This will keep its integrity visually, if it is just for display purposes, otherwise you will need to re-cast appropriately in JS (in which case use Shivaji's answer).
JavaScript Date's object will return the DATE object and it's POSITION that is being assigned in your computer. So, when you are working with a date or datetime types, you can use some of the methods that are provided by the Date object, such as getDate() and getDay(). But, a better solution would be to format the Date object itself. For example: use the toString() or toUTCString() methods.
var d = new Date();
document.getElementById("demo").innerHTML = d.toString();
Reference:
https://www.w3schools.com/js/js_date_formats.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
I am not so into JavaScript and I have the following problem.
I have a JSON object like this:
{
"start_date": "2017-11-09 06:00:00"
}
Into a JavaScript script executed into the browser I do:
var dateCurrentOriginalForecast = new Date(currentOriginalForecast.start_date);
and it works fine: it creates a new Date object with the value related to 2017-11-09 06:00:00 date.
The problem is that I have to perform this JavaScript script into a Java application using Rhino (a JavaScript implementation that allows to perform JS code into a Java application) and here it cause an error:
TID: [-1234] [] [2017-11-09 11:10:08,915] INFO {org.apache.synapse.mediators.bsf.ScriptMessageContext} - dateCurrentOriginalForecast: Invalid Date {org.apache.synapse.mediators.bsf.ScriptMessageContext}
TID: [-1234] [] [2017-11-09 11:10:08,918] ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} - The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: RangeError: Date is invalid. (<Unknown Source>#137) in <Unknown Source> at line number 137
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
at javax.script.CompiledScript.eval(CompiledScript.java:92)
It seems that this date is invalid and it can't create the Date object.
From what I understood reading online the problem should be that old JS or Rhino (maybe the version of JS implemented by Rhino) does not support date of this type and probably I have to convert it in a date format which is fully compliant with ISO 8601
So I think that I have to convert my string 2017-11-09 06:00:00 into something like compliant with ISO 8601 standard.
I can't use third party library.
How can I do it?
Can use Date#toISOString() or Date#toJSON()
let d = new Date('2017-11-09 06:00:00')
console.log(d.toISOString())
console.log(d.toJSON())
//if you want convert date without convert in timezone than
var date = '2017-11-09 06:00:00';
var convertDate = date.replace(" ", "T"); // 2017-11-09T06:00:00
//if you want to convert in date with utc timezone
var date = new Date("2017-11-09 06:00:00").toISOString()
If I've understood your question correctly the problem is not so much that you need a ISO 8601 formatted date, but it is that you need to create a Date object from a date that is not formatted in ISO 8601. I personally would just use regular expression to parse the date into it's parts and then pass them into the Date constructor:
var currentOriginalForecast = {
"start_date": "2017-11-09 06:00:00"
};
var rxParseDate = /(\d{4})-(\d\d)-(\d\d)\s+(\d\d):(\d\d):(\d\d)/;
var dateParts = currentOriginalForecast.start_date.match(rxParseDate);
var year = dateParts[1],
month = dateParts[2],
day = dateParts[3],
hour = dateParts[4],
minute = dateParts[5],
second = dateParts[6];
var dateCurrentOriginalForecast = new Date(Date.UTC(year, month - 1, day, hour, minute, second));
console.log(dateCurrentOriginalForecast);
Since there is no timezone mentioned in the start_date, I'm assuming it is UTC and converting it using Date.UTC and passing the resulting timestamp from that into the Date constructor. If start_date is in local time you would just remove Date.UTC and pass the parameters directly into the Date constructor. I'll also mention the month - 1; that is because the Date constructor (and Date.UTC) expect a 0-based month.