Get the locale aware default format string of moment().format() - momentjs

The moment().format('L') returns date formatted as per set locale. For example, by default, it will return 01/31/2019 for en and 31/01/2019 for fr. I need to get that format (such as MM/DD/YYYY for en and DD/MM/YYYY for fr). How to get that format from moment?

You can use moment localeData and longDateFormat to get locale aware format tokens used by moment.
Here a live sample:
console.log( moment.localeData('en').longDateFormat('L') ); // MM/DD/YYYY
console.log( moment.localeData('fr').longDateFormat('L') ); // DD/MM/YYYY
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
Don't forget to load required locales in your enviroment (see i18n section).

Yes this is accessible via creationData:
console.log(moment().creationData().locale._longDateFormat.L)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

Related

How to change date format in Google Forms Response?

I have a Google Forms to get my delivery order from customer and on the forms I have Date field.
The response of the forms will be filled automatically to order document per response.
I use this scripts:
function autoFill(e) {
var timestamp = e.values[0];
var nama = e.values[1];
var tglBuat = e.values[10];
var file = DriveApp.getFileById(MY_TEMPLATE_FILE_ID);
var folder = DriveApp.getFolderById(OUTPUT_FOLDER_ID);
var copy = file.makeCopy(nama+"_"+timestamp, folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
body.replaceText('#NamaLengkap#', tglBuat);
body.replaceText('#TanggalDibuat#', tglBuat);
doc.saveAndClose();
}
The flow is simple like this:
I prepared Template file for the Order Document paper
Customer will fill the forms
Form result will be kept in certain Google Spreadsheet
The script above on the (3), will be triggered everytime (2) submitted
Voila, I have Order Document filled with customer order details
My template file are something like this:
Customer Name: #NamaLengkap#
Order Date: #TanggalDibuat#
My problem is here in date format, I want the output on my template file using this format "26 August 2020", but the google form only give this format "08/26/2020".
How do I changes it?
I read some article about changing the email format before filling the form, but i don't think this is good solution. Because customer wont care at all.
Solution
You just need to take your date and convert it to a Date String Javascript object as shown below:
function myFunction() {
var shortDate = new Date("03/25/2015");
var longDateFormat = shortDate.toDateString();
Logger.log(longDateFormat);
}
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

How to convert Firebase Firestore timestamp to Zulu datetime format in Firebase Function

I have a timestamp field in Firestore document. (refer to created_at)
I want to pass it to the server a Datetime Zulu time format like below.
2019-10-31T10:29:45Z
How do I convert Firestore timestamp to a Zulu timeformat string ?
here is my onCreate in Firebase function, upon creating a document in firestore, I read the timestamp field and print to console
exports.createRecord = functions.firestore
.document(`record/{recordId}`)
.onCreate(async (snap, context) => {
console.log(snap.data().created_at);
});
This will display in the Firebase Function logs
Timestamp { _seconds: 1573496322, _nanoseconds: 897429000 }
How can I convert this to look like in this form:
2019-11-01T01:36:56.233018Z
2019-01-01T00:00:00Z
2019-10-31T10:29:45Z
Firestore Timestamp object is not an extension of the Javascript Date object. But it contains a toDate method to get a regular Date object, which you can use to easily format the date that you need into a string.
Cloud functions run with no timezone offset (UTC time). So you effectively get Zulu time when you format your dates. Use toISOString to get a string on the wanted format
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".
Try to change your code above to this and you will see the output that you expect.
exports.createRecord = functions.firestore
.document(`record/{recordId}`)
.onCreate(async (snap, context) => {
console.log(snap.data().created_at.toDate().toISOString());
});
Additional resources
Moment JS is a great and easy to use library if you have more complex need to manipulate dates or work with timezones. It adds some overhead to your function dependenceis, but it really helps readability of your code.

Unregister format types

I'm trying to unregister some format types (core/code, core/strikethrough).
I tried the unregisterFormatType function. It doesn't work and console says the types don't exist.
Format core/code is not registered.
The line looks like this:
const {unregisterFormatType} = wp.richText;
unregisterFormatType("core/code");
What am I doing wrong?
I think you have to wait until the format is registered before you deregister it.
So something like this would work:
wp.domReady(function () {
wp.richText.unregisterFormatType('core/code')
})

Change IETF format to unix timestamp

Hi is there a way to change the time format for the following snipp in fullcalendar?
select: function(startDate, endDate) {
$.fancybox({
\'width\': \'40%\',
\'height\': \'40%\',
\'autoScale\': true,
\'transitionIn\': \'fade\',
\'transitionOut\': \'fade\',
\'type\': \'iframe\',
\'href\': \'test.php/?start=\'+startDate+\'&end=\'+endDate,
});
calendar.fullCalendar(\'unselect\');
}
I want Start & EndDate to be a unix Timestamp.
Thank You
It can be achieved by overriding the "start" and "end" parameter by sending separating Ajax Request inside the FullCalendar.
Take a look at this Similar Thread: add custom params to fullcalendar request

How to convert Ajax serialized dates to date format in sencha touch

I am building an App using Sencha touch api.
I have a model
Ext.regModel('Task',
{fields: [{name:'TaskID', type:'int'},
{name:'DueDate', type:'date'},
{name:'ClientName', type:'string'},
{name:'TaskName', type:'string'},
{name:'AssignedTo', type:'string'}]
});
I recieve a date /Date(1304879400000)/ i.e Ajax serialized date. So how do i convert into readable date format. Any help would be appreciated.
Thanks
Use a converter in the model config:
fields: [
'id'
{
name: 'datetime',
type: 'date',
dateFormat: 'MS'
}
The above model has two fields: id and datetime, with datetime being parsed as a Microsoft serialised Ajax string.
See docs for dateFormat and the 'MS' format.
The number is the timestamp so you could parse it like:
date = new Date(parseInt(DueDate.substr(6)));
Where DueDate is your "/Date(1304879400000)/" formatted string

Resources