Date Created timestamp issues

Hi Guys,

How on earth do we remove all off the rubbish from the Date & Time Created stamp?

Currently it shows like this:

Mon Jan 01 2018 00:00:00 GMT+0000 (GMT)

Basically i want it to look like this:

1 January 2018 | 9:40

Hi Robert. I had this and had it solved. My data came from a table. Also I’m setting the fields through quering the API and then setting them, not linking them on the page.

In this way I get a date format and it will through an error. So if you add ‘toString()’ you get what you have.

$w("#txtSample").text = itemData.trail_at.toString();

So, itemData is a query result from the API. ‘trail_at’ is a date field. You need ‘toString()’ for you are putting it in text.

In the same line I can use standard javascript items to get date parts:

$w("#txtSample").text = itemData.trail_at.getFullYear().toString(); // the year
$w("#txtSample").text = itemData.trail_at.getMonth().toString(); // the month, carefull it may start at 0
$w("#txtSample").text = itemData.trail_at.getDate().toString();  // the date

See for more info here or search for javascript get a date part.

So - how do i implement this? Or can the Wix Code team provide a solution

I think I can give an example. Can you say if you are using a repeater as I expect? Then I can answer based on that, but it would be tommorrow

Yeah repeater! :slight_smile:

Ah I had some battery power left. These are the steps to follow.

  1. Add a element in which you want to display the date. Do not link it to a column
  2. Go to the page code
  3. Add this to the ‘onReady’ function of the page
$w("#repeaterTrailList").onItemReady( ($w, itemData, index) => {
		const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

		console.log(itemData.trail_at);		
		console.log(itemData.trail_at.getDate().toString());
		console.log(monthNames[itemData.trail_at.getMonth()]);
		console.log(itemData.trail_at.getFullYear().toString());
		console.log(itemData.trail_at.getHours().toString());
		console.log(itemData.trail_at.getMinutes().toString());
		
		const strDate = itemData.trail_at.getDate().toString();
		const strMonth = monthNames[itemData.trail_at.getMonth()];
		const strYear = itemData.trail_at.getFullYear().toString();
		const strHour = ("0" + itemData.trail_at.getHours().toString()).slice(-2);
		const strMinute = ("0" + itemData.trail_at.getMinutes().toString()).slice(-2);
		
		$w("#text11").text = strDate + " " + strMonth + " " + strYear + " | " + strHour + ":" + strMinute;
		
	});

Explanation:
My repeater is call ‘#repeaterTrailList’ replace with your repeater name. Then you need to add an array of months. Standard javascript can’t return those. I haven’t been able to implement momentjs yet, then it would be easier. Next there are console log line to show you how to get each part you want. Finally I set the text element ‘#text11’ wit the format in you initial post.

2 Likes

Hey man, thanks so much! It’s not working though - when i view live it’s not showing anything?

What does the console line show? Can you post the code where you put it, and a screenshot of the console when you preview it, and/or the console log of the live site? console lines will appear in e.g. chrome F12 debugger tab console.

Ah. There are two things. The first you see now, the second you would get later. The code must be in the onReady function. Place everything from line 11 to 29 between e.g. 8 and 9.

Also, change the column ‘trail_at’ with your column with the date in the table,

Here is the code for removing zeros for a text field linked to the date field of your dataset:
$w.onReady(function () {
$w(“#myDataset”).onReady(()=>{
let itemObj = $w(“#myDataset”).getCurrentItem();
$w(“#text1”).text = itemObj.valuenameofdatefield.toDateString();
});
});
Create a onReady event for your dataset, and then change the “#myDataset” to your dataset’s name, the #text1 to your text’s id name and the valuenameofdatefield to the value name of the date field you are connecting to. Here’s a quick video too.

However Wix Code team, if I try this with a repeater, it does indeed remove the zero’s but it is only bringing up the date of the first repeater’s date across all the other boxes. Should I be modifying the code in some way or is this a glitch? Would love to get an answer because I plan to use this so much!

2 Likes

Yes Lorraine, you are right…using the above code works with Dynamic Pages and partially works with Repeaters.

With repeaters the above code formats the date, but it’s the same date that shows in every item inside the repeater? Is there a solution or are we doing something wrong.

Hey guys. What part is not correct then in a repeater? I have a repeater on my site, see print bellow, and format the date too, and hides the section if there is no date. I do it the same way, only not with named months and so on.

Here’s the example- I wonder why its working for you but not for us? Was the date already on the repeater originally or you added a text block to the repeater?

No, I assign the repeater data with the API for it’s multilingual. It can also be your ‘getCurrentItem’. I see you are doing it on a dataset ready and I’m doing it on item ready. That will be a major difference. I’ll read about it tonight see if I see something

Thanks. I wish I knew enough to know the difference. I appreciate the help.

Edgar,

If I pay you, will you go into my editor and implement this? :slight_smile:

Hi Lorraine, Robert,

I made a page with standard objects on an existing table of mine. So the result is this:


The first field is the standard date/time field the second is the ‘converted’ datetime to text. The third is the code from Lorraine.

// For full API documentation, including code examples, visit http://wix.to/94BuAAs

$w.onReady(function () {
	// FIELD TWO
	$w("#repeater1").onItemReady( ($w, itemData, index) => {
		const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

		console.log(itemData.trail_at);		
		console.log(itemData.trail_at.getDate().toString());
		console.log(monthNames[itemData.trail_at.getMonth()]);
		console.log(itemData.trail_at.getFullYear().toString());
		console.log(itemData.trail_at.getHours().toString());
		console.log(itemData.trail_at.getMinutes().toString());
		
		const strDate = itemData.trail_at.getDate().toString();
		const strMonth = monthNames[itemData.trail_at.getMonth()];
		const strYear = itemData.trail_at.getFullYear().toString();
		const strHour = ("0" + itemData.trail_at.getHours().toString()).slice(-2);
		const strMinute = ("0" + itemData.trail_at.getMinutes().toString()).slice(-2);
		
		$w("#text13").text = strDate + " " + strMonth + " " + strYear + " | " + strHour + ":" + strMinute;
		
	});
	
	
	// FIELD THREE
	$w("#dynamicDataset").onReady(()=>{ 
        let itemObj = $w("#dynamicDataset").getCurrentItem(); 
        $w("#text15").text = itemObj.trail_at.toDateString(); 
    });


});

All the code is above. In this code ‘trail_at’ can be any date/time field.

So first for Lorraine, you are using the dataset data. Your '#myDataset ’ is my ‘#dynamicDataset’. I’m using the repeater data ‘repeater1’ which triggers the code when each item in it is ready (say displayed). That’s the difference. I think you can you the dataset item too, but then you need to change more of the code and I don’t see some called ‘forEach’ to make a loop. So I suggest to switch to the repeater object.

Then for Lorraine and Robert, sure I can help make adjustments like Robert asks, not a problem. Do check my profile though. Also you will need to pass a mail address, I don’t put mine in posts, and the form lacks a pm function. Some security tip if you pass a mail address in a forum, don’t do it ‘attached’ but use spaces or put it in an image. This way sniffers can’t get it so easily.

3 Likes

Can you email me? I’ve seperated my address

Robert

@

Ayrshire

Daily news

.co.uk

What if I want the date to display in Spanish?